plt17_plot_in_plot.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. # 17 - plot in plot
  5. """
  6. Please note, this script is for python3+.
  7. If you are using python2+, please modify it accordingly.
  8. Tutorial reference:
  9. http://www.python-course.eu/matplotlib_multiple_figures.php
  10. """
  11. import matplotlib.pyplot as plt
  12. fig = plt.figure()
  13. x = [1, 2, 3, 4, 5, 6, 7]
  14. y = [1, 3, 4, 2, 5, 8, 6]
  15. # below are all percentage
  16. left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
  17. ax1 = fig.add_axes([left, bottom, width, height]) # main axes
  18. ax1.plot(x, y, 'r')
  19. ax1.set_xlabel('x')
  20. ax1.set_ylabel('y')
  21. ax1.set_title('title')
  22. ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25]) # inside axes
  23. ax2.plot(y, x, 'b')
  24. ax2.set_xlabel('x')
  25. ax2.set_ylabel('y')
  26. ax2.set_title('title inside 1')
  27. # different method to add axes
  28. ####################################
  29. plt.axes([0.6, 0.2, 0.25, 0.25])
  30. plt.plot(y[::-1], x, 'g')
  31. plt.xlabel('x')
  32. plt.ylabel('y')
  33. plt.title('title inside 2')
  34. plt.show()