plotting.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: huiming zhou
  5. """
  6. import matplotlib.pyplot as plt
  7. import env
  8. class Plotting():
  9. def __init__(self, xI, xG):
  10. self.xI, self.xG = xI, xG
  11. self.env = env.Env()
  12. self.obs = self.env.obs_map()
  13. def animation(self, path, visited, name):
  14. self.plot_grid(name)
  15. self.plot_visited(visited)
  16. self.plot_path(path)
  17. def plot_grid(self, name):
  18. obs_x = [self.obs[i][0] for i in range(len(self.obs))]
  19. obs_y = [self.obs[i][1] for i in range(len(self.obs))]
  20. plt.plot(self.xI[0], self.xI[1], "bs")
  21. plt.plot(self.xG[0], self.xG[1], "gs")
  22. plt.plot(obs_x, obs_y, "sk")
  23. plt.title(name)
  24. plt.axis("equal")
  25. def plot_visited(self, visited):
  26. visited.remove(self.xI)
  27. count = 0
  28. for x in visited:
  29. count += 1
  30. plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
  31. plt.gcf().canvas.mpl_connect('key_release_event', lambda event:
  32. [exit(0) if event.key == 'escape' else None])
  33. if count < len(visited) / 3:
  34. length = 15
  35. elif count < len(visited) * 2 / 3:
  36. length = 30
  37. else:
  38. length = 45
  39. if count % length == 0: plt.pause(0.001)
  40. def plot_path(self, path):
  41. path.remove(self.xI)
  42. path.remove(self.xG)
  43. path_x = [path[i][0] for i in range(len(path))]
  44. path_y = [path[i][1] for i in range(len(path))]
  45. plt.plot(path_x, path_y, linewidth='3', color='r', marker='o')
  46. plt.pause(0.01)
  47. plt.show()