plotting.py 1.5 KB

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