plt15_subplot.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. # 15 - subplot
  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.scipy-lectures.org/intro/matplotlib/matplotlib.html
  10. """
  11. import matplotlib.pyplot as plt
  12. # example 1:
  13. ###############################
  14. plt.figure(figsize=(6, 4))
  15. # plt.subplot(n_rows, n_cols, plot_num)
  16. plt.subplot(2, 2, 1)
  17. plt.plot([0, 1], [0, 1])
  18. plt.subplot(222)
  19. plt.plot([0, 1], [0, 2])
  20. plt.subplot(223)
  21. plt.plot([0, 1], [0, 3])
  22. plt.subplot(224)
  23. plt.plot([0, 1], [0, 4])
  24. plt.tight_layout()
  25. # example 2:
  26. ###############################
  27. plt.figure(figsize=(6, 4))
  28. # plt.subplot(n_rows, n_cols, plot_num)
  29. plt.subplot(2, 1, 1)
  30. # figure splits into 2 rows, 1 col, plot to the 1st sub-fig
  31. plt.plot([0, 1], [0, 1])
  32. plt.subplot(234)
  33. # figure splits into 2 rows, 3 col, plot to the 4th sub-fig
  34. plt.plot([0, 1], [0, 2])
  35. plt.subplot(235)
  36. # figure splits into 2 rows, 3 col, plot to the 5th sub-fig
  37. plt.plot([0, 1], [0, 3])
  38. plt.subplot(236)
  39. # figure splits into 2 rows, 3 col, plot to the 6th sub-fig
  40. plt.plot([0, 1], [0, 4])
  41. plt.tight_layout()
  42. plt.show()