plotting.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(self.xI, self.xG)
  7. self.obs = self.env.obs_map()
  8. self.lose = self.env.lose_map()
  9. def animation(self, path, name):
  10. """
  11. animation.
  12. :param path: optimal path
  13. :param name: tile of figure
  14. :return: an animation
  15. """
  16. plt.figure(1)
  17. self.plot_grid(name)
  18. self.plot_lose()
  19. self.plot_path(path)
  20. def plot_grid(self, name):
  21. """
  22. plot the obstacles in environment.
  23. :param name: title of figure
  24. :return: plot
  25. """
  26. obs_x = [self.obs[i][0] for i in range(len(self.obs))]
  27. obs_y = [self.obs[i][1] for i in range(len(self.obs))]
  28. plt.plot(self.xI[0], self.xI[1], "bs")
  29. for x in self.xG:
  30. plt.plot(x[0], x[1], "gs")
  31. plt.plot(obs_x, obs_y, "sk")
  32. plt.title(name)
  33. plt.axis("equal")
  34. def plot_lose(self):
  35. """
  36. plot losing states in environment.
  37. :return: a plot
  38. """
  39. lose_x = [self.lose[i][0] for i in range(len(self.lose))]
  40. lose_y = [self.lose[i][1] for i in range(len(self.lose))]
  41. plt.plot(lose_x, lose_y, color='#A52A2A', marker='s')
  42. def plot_visited(self, visited):
  43. """
  44. animation of order of visited nodes.
  45. :param visited: visited nodes
  46. :return: animation
  47. """
  48. visited.remove(self.xI)
  49. count = 0
  50. for x in visited:
  51. count += 1
  52. plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
  53. plt.gcf().canvas.mpl_connect('key_release_event', lambda event:
  54. [exit(0) if event.key == 'escape' else None])
  55. if count < len(visited) / 3:
  56. length = 15
  57. elif count < len(visited) * 2 / 3:
  58. length = 30
  59. else:
  60. length = 45
  61. if count % length == 0: plt.pause(0.001)
  62. def plot_path(self, path):
  63. path.remove(self.xI)
  64. for x in self.xG:
  65. if x in path:
  66. path.remove(x)
  67. for x in path:
  68. plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
  69. plt.gcf().canvas.mpl_connect('key_release_event', lambda event:
  70. [exit(0) if event.key == 'escape' else None])
  71. plt.pause(0.001)
  72. plt.show()
  73. plt.pause(0.5)
  74. def plot_diff(self, diff, name):
  75. plt.figure(2)
  76. plt.title(name, fontdict=None)
  77. plt.xlabel('iterations')
  78. plt.ylabel('difference of successive iterations')
  79. plt.grid('on')
  80. count = 0
  81. for x in diff:
  82. plt.plot(count, x, color='#808080', marker='o') # plot dots for animation
  83. plt.gcf().canvas.mpl_connect('key_release_event', lambda event:
  84. [exit(0) if event.key == 'escape' else None])
  85. plt.pause(0.07)
  86. count += 1
  87. plt.plot(diff, color='#808080')
  88. plt.pause(0.01)
  89. plt.show()