plotting.py 3.1 KB

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