matplotlib库是专门用于开发2D图表(包括3D图表)的,近年来被广泛用于科技圈。
安装:
1.Anaconda
conda installmatplotlib
2.Debian-Ubuntu Linux
sudoapt-get install python-matplotlib
3.Fedora-Redhat Linux
sudoyum install python-matplotlib
更多内容可以访问我的个人网站https://www.cjluzzl.cn
架构
1.Scripting 脚本层
2.Artist 表现层
3.Backend 模型层
Backend层
matplotlibAPI位于该层,这些API是用来在底层实现图形元素的一个个类。
FigureCanvas 对象实现了绘图区域这一概念
Renderer 对象在FigureCanvas上绘图
Event 对象处理用户输入(键盘和鼠标事件)
Artist层
中间层为Artist层。图层中所有能看到的元素都属于Artist对象,即标题、轴标签、刻度等组成图形所有元素的都是Artist对象的实例。图形中每个元素的实例在层级结构中有着自己的位置
Artist类分为两种:原始(primitive)和复合(composite)
Scripting层
包含pyplot借口
生成一幅简单的交互式图表
In [1]: import matplotlib.pyplot as plt
In [2]: plt.plot([1,2,3,4])
Out[2]: [<matplotlib.lines.Line2D at0x6578908>]
In [3]: plt.show()
设置图形属性
1.设置坐标轴取值范围
plt.axis([0,5,0,20])
传入一个列表,四个值分别为[xmin,xmax,ymin,ymax]用来指定x轴和y轴的取值范围
2.设置标题
plt.title(“Myfirst plot”)
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()
matplotlib 与 numpy
In [12]: import math
In [13]: import numpy as np
In [14]: t = np.arange(0,2.5,0.1)
In [15]: y1 = map(math.sin,math.pi*t)
In [16]: y2 =map(math.sin,math.pi*t+math.pi/2)
In [17]: y3 =map(math.sin,math.pi*t-math.pi/2)
In [18]:plt.plot(t,y1,'b*',t,y2,'g^',t,y3,'ys')
Out[18]:
[<matplotlib.lines.Line2D at0xa1d9780>,
<matplotlib.lines.Line2D at 0xa1d9828>,
<matplotlib.lines.Line2D at 0xa1d9c18>]
In [19]: plt.show()