matplotlib的常用的两种方式以及pylab

来源:互联网 发布:淘宝规则2016 编辑:程序博客网 时间:2024/05/17 06:08


pylab不推荐使用的原因:pylab更接近于MATLAB,这是毋庸置疑的,但是使用pylab会逐渐背离matplotlib的学习,这与初衷想反,当然,还有其他的原因,没有研究。


三种方式的实现代码(相对来说 matplotlib 实现更简单)


pyplot的方式

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  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(0101)  
  8. y = np.random.randn(len(x))  
  9.   
  10. plt.title("pyplot")  
  11.   
  12. plt.plot(x, y)  
  13. plt.show()  


pylab的方式

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/python  
  2. #coding: utf-8  
  3.   
  4. from pylab import *  
  5.   
  6. x = arange(0101)  
  7. y = randn(len(x))  
  8.   
  9. title("pylab")  
  10. plot(x, y)  
  11. show()  


类封装的方式

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  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(0101)  
  8. y = np.random.randn(len(x))  
  9.   
  10. # 生成一个figure对象  
  11. fig = plt.figure()  
  12. ax = fig.add_subplot(111)  
  13.   
  14. plt.plot(x, y)  
  15.   
  16. ax.set_title("object oriented")  
  17.   
  18. plt.show()  

0 0
原创粉丝点击