python 画子图(add_subplot & subplot)

原文地址:点击打开链接


子图:就是在一张figure里面生成多张子图。

Matplotlib对象简介

   FigureCanvas  画布

   Figure        图

   Axes          坐标轴(实际画图的地方)




注意,pyplot的方式中plt.subplot()参数和面向对象中的add_subplot()参数和含义都相同。


使用面向对象的方式

[python]  view plain  copy
  1. #!/usr/bin/python  
  2. #coding: utf-8  
  3.   
  4. import numpy as np  
  5. import matplotlib.pyplot as plt  
  6.   
  7. x = np.arange(0100)  
  8.   
  9. fig = plt.figure()  
  10.   
  11. ax1 = fig.add_subplot(221)  
  12. ax1.plot(x, x)  
  13.   
  14. ax2 = fig.add_subplot(222)  
  15. ax2.plot(x, -x)  
  16.   
  17. ax3 = fig.add_subplot(223)  
  18. ax3.plot(x, x ** 2)  
  19.   
  20. ax4 = fig.add_subplot(224)  
  21. ax4.plot(x, np.log(x))  
  22.   
  23. plt.show()  




pyplot的方式

[python]  view plain  copy
  1. #!/usr/bin/python  
  2. #coding: utf-8  
  3.   
  4. import numpy as np  
  5. import matplotlib.pyplot as plt  
  6.   
  7. x = np.arange(0100)  
  8.   
  9. plt.subplot(221)  
  10. plt.plot(x, x)  
  11.   
  12. plt.subplot(222)  
  13. plt.plot(x, -x)  
  14.   
  15. plt.subplot(223)  
  16. plt.plot(x, x ** 2)  
  17.   
  18. plt.subplot(224)  
  19. plt.plot(x, np.log(x))  
  20.   
  21. plt.show()  

四个子图的顺序为从从左至右,从上到下依次为1,2,3,4





猜你喜欢

转载自blog.csdn.net/michaelhan3/article/details/74019034
今日推荐