学习《matplotlib for python Developers》笔记
python中使用matplotlib通常有以下三种方式
1,通过使用pyplot模块,提供类似Matlab命令的方式。
2,pylab模块,集合Matplotlib和Numpy使之接近Matlab(不被作者提倡)
3,面向对象方法,以python的方式使用,更加的pythonic。
三者之间面向对象的方法比价复杂,但是可以和GUI很好的融合。下面主要介绍第三种方法。使用实例如下:
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,10,0.1)
y=np.random.randn(len(x))
fig=plt.figure()
ax=fig.add_subplot(111)
l,=plt.plot(x,y)
t=ax.set_title('random numbers')
plt.show()对于plot返回的是一个line2D的对象,故能在line2D的操作都使用于l。如:
l.set_color('red')#设置颜色
figure()返回一个Figure对象
add_subplot返回一个Axes对象
Matplotlib对象介绍
Object |
Description |
| FigureCanvas | Container class for the Figure instance |
| Figure | Container for one or more Axes instances |
| Axes | The rectangular ares to hold the basic elements,such as lines,text,and so on |
本文详细介绍了在Python中使用matplotlib的三种方式:通过pyplot模块实现类Matlab命令风格,pylab模块整合Matplotlib和Numpy类似Matlab操作但不推荐使用,以及面向对象方法实现更Pythonic的绘图流程。重点讲解了面向对象方法的使用,并提供了实例代码,展示了如何创建Figure、Axes对象以及使用plot方法绘制图表。此外,文章还介绍了matplotlib中的关键对象和其功能,为开发者提供了全面的指南。
3882

被折叠的 条评论
为什么被折叠?



