plotting.py 3.1 KB

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