plotting.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. Plot tools 2D
  3. @author: huiming zhou
  4. """
  5. import os
  6. import sys
  7. import matplotlib.pyplot as plt
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
  9. "/../../Search-based Planning/")
  10. from Search_2D import env
  11. class Plotting:
  12. def __init__(self, xI, xG):
  13. self.xI, self.xG = xI, xG
  14. self.env = env.Env()
  15. self.obs = self.env.obs_map()
  16. def animation(self, path, visited, name):
  17. self.plot_grid(name)
  18. self.plot_visited(visited)
  19. self.plot_path(path)
  20. plt.show()
  21. def plot_grid(self, name):
  22. obs_x = [self.obs[i][0] for i in range(len(self.obs))]
  23. obs_y = [self.obs[i][1] for i in range(len(self.obs))]
  24. plt.plot(self.xI[0], self.xI[1], "bs")
  25. plt.plot(self.xG[0], self.xG[1], "gs")
  26. plt.plot(obs_x, obs_y, "sk")
  27. plt.title(name)
  28. plt.axis("equal")
  29. def plot_visited(self, visited, cl='gray'):
  30. if self.xI in visited:
  31. visited.remove(self.xI)
  32. if self.xG in visited:
  33. visited.remove(self.xG)
  34. count = 0
  35. for x in visited:
  36. count += 1
  37. plt.plot(x[0], x[1], linewidth='3', color=cl, marker='o')
  38. plt.gcf().canvas.mpl_connect('key_release_event',
  39. lambda event: [exit(0) if event.key == 'escape' else None])
  40. if count < len(visited) / 3:
  41. length = 15
  42. elif count < len(visited) * 2 / 3:
  43. length = 25
  44. else:
  45. length = 35
  46. if count % length == 0:
  47. plt.pause(0.001)
  48. plt.pause(0.01)
  49. def plot_path(self, path, cl='r', flag=False):
  50. if self.xI in path:
  51. path.remove(self.xI)
  52. if self.xG in path:
  53. path.remove(self.xG)
  54. path_x = [path[i][0] for i in range(len(path))]
  55. path_y = [path[i][1] for i in range(len(path))]
  56. if not flag:
  57. plt.plot(path_x, path_y, linewidth='3', color='r', marker='o')
  58. else:
  59. plt.plot(path_x, path_y, linewidth='3', color=cl, marker='o')
  60. plt.pause(0.01)
  61. def animation_ara_star(self, path, visited, name):
  62. self.plot_grid(name)
  63. cl_v, cl_p = self.color_list()
  64. for k in range(len(path)):
  65. self.plot_visited(visited[k], cl_v[k])
  66. self.plot_path(path[k], cl_p[k], True)
  67. plt.pause(0.5)
  68. plt.show()
  69. @staticmethod
  70. def color_list():
  71. cl_v = ['silver', 'wheat', 'lightskyblue', 'plum', 'slategray']
  72. cl_p = ['gray', 'orange', 'deepskyblue', 'red', 'm']
  73. return cl_v, cl_p