plotting.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: huiming zhou
  5. """
  6. import matplotlib.pyplot as plt
  7. def animation(xI, xG, obs, path, visited, name):
  8. """
  9. generate animation for exploring process of algorithm
  10. :param xI: starting state
  11. :param xG: goal state
  12. :param obs: obstacle map
  13. :param path: optimal path
  14. :param visited: visited nodes
  15. :param name: name of this figure
  16. :return: animation
  17. """
  18. visited.remove(xI)
  19. path.remove(xI)
  20. path.remove(xG)
  21. # plot gridworld
  22. obs_x = [obs[i][0] for i in range(len(obs))]
  23. obs_y = [obs[i][1] for i in range(len(obs))]
  24. plt.plot(xI[0], xI[1], "bs")
  25. plt.plot(xG[0], xG[1], "gs")
  26. plt.plot(obs_x, obs_y, "sk")
  27. plt.title(name)
  28. plt.axis("equal")
  29. # animation for the exploring order of visited nodes
  30. count = 0
  31. for x in visited:
  32. count += 1
  33. plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
  34. plt.gcf().canvas.mpl_connect('key_release_event',
  35. lambda event: [exit(0) if event.key == 'escape' else None])
  36. if count < 500: length = 20
  37. elif count < 700: length = 30
  38. else: length = 50
  39. if count % length == 0: plt.pause(0.001)
  40. # plot optimal path
  41. path_x = [path[i][0] for i in range(len(path))]
  42. path_y = [path[i][1] for i in range(len(path))]
  43. plt.plot(path_x, path_y, linewidth='3', color='r', marker='o')
  44. # show animation
  45. plt.pause(0.01)
  46. plt.show()