plotting.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 % 20 == 0: plt.pause(0.01)
  37. # plot optimal path
  38. path_x = [path[i][0] for i in range(len(path))]
  39. path_y = [path[i][1] for i in range(len(path))]
  40. plt.plot(path_x, path_y, linewidth='3', color='r', marker='o')
  41. # show animation
  42. plt.pause(0.01)
  43. plt.show()