-
说明
matplotlib 是面向对象的绘图工具包,绘制的图形中每个元素都是一个对象
-
安装
sudo pip3 install matplotlib
-
引入
import matplotlib.pyplot as plt
# 如遇报错:ImportError: No module named '_tkinter'
# 终端执行:sudo apt install -y python3-tk
-
使用
创建画布和子图:
In [19]: fig = plt.figure() # figure 方法创建一张画布
In [20]: ax1 = fig.add_subplot(2, 2, 1) # add_subplot 方法创建一个子图
In [21]: fig
Out[21]: <Figure size 640x480 with 1 Axes>
In [22]: ax1
Out[22]: <matplotlib.axes._subplots.AxesSubplot at 0x7f077b31b978>
In [23]: plt.gcf() # 等于最近创建的画布 fig
Out[23]: <Figure size 640x480 with 1 Axes>
In [24]: plt.gca() # 等于最近创建的子图 ax1
Out[24]: <matplotlib.axes._subplots.AxesSubplot at 0x7f077b31b978>
In [25]: ax2 = fig.add_subplot(2, 2, 2) # 继续创建仨子图
In [26]: ax3 = fig.add_subplot(2, 2, 3)
In [27]: ax4 = fig.add_subplot(2, 2, 4)
在子图上作画:
# plot 方法绘制线形图,50 个点的点划线,第二个参数设置线的样式
In [28]: ax1.plot(np.random.randn(50).cumsum(), 'k--')
Out[28]: [<matplotlib.lines.Line2D at 0x7f077aaa4390>]
# hist 方法绘制直方图,100 个数,分成 20 组,颜色为绿色
In [29]: ax2.hist(np.random.randn(100), bins=20, color='g')
Out[29]:
(array([ 1., 2., 1., 2., 8., 1., 4., 9., 11., 12., 10., 4., 9.,
6., 10., 3., 2., 1., 3., 1.]),
array([-2.61999253, -2.35870027, -2.09740801, -1.83611575, -1.57482349,
-1.31353123, -1.05223897, -0.79094671, -0.52965445, -0.26836219,
-0.00706993, 0.25422233, 0.51551459, 0.77680685, 1.03809911,
1.29939136, 1.56068362, 1.82197588, 2.08326814, 2.3445604 ,
2.60585266]),
<a list of 20 Patch objects>)
# scatter 方法绘制散点图,第一个参数为 x 轴的值;第二个参数为 y 轴的值
In [30]: ax3.scatter(np.arange(33), np.arange(33)+3*np.random.randn(33))
Out[30]: <matplotlib.collections.PathCollection at 0x7f077aa59a20>
In [32]: spread = np.random.rand(50) * 100 # 产生 50 个 [0, 100) 的随机数
In [33]: center = np.ones(25) * 50 # 产生 25 个值为 50 的数
In [34]: outlier_high = np.random.rand(10) * 100 + 100 # 产生 10 个异常值
In [35]: outlier_low = np.random.rand(10) * -100 # 同上
# boxplot 方法绘制箱线图,参数是上面 4 组数用 concatenate 方法拼接的结果
In [36]: ax4.boxplot(np.concatenate(
(spread, center, outlier_high, outlier_low)
))
Out[36]:
{'boxes': [<matplotlib.lines.Line2D at 0x7f077aa2a198>],
'caps': [<matplotlib.lines.Line2D at 0x7f077aa3f940>,
<matplotlib.lines.Line2D at 0x7f077aa3f9b0>],
'fliers': [<matplotlib.lines.Line2D at 0x7f077aa3ffd0>],
'means': [],
'medians': [<matplotlib.lines.Line2D at 0x7f077aa3fba8>],
'whiskers': [<matplotlib.lines.Line2D at 0x7f077aa1e550>,
<matplotlib.lines.Line2D at 0x7f077aa52ba8>]}
In [37]: fig.show() # 查看结果如下图
线形图 直方图 散点图 箱线图
-
pandas 绘图之术
直接调用matplotlib.pyplot
模块相关方法就可以完成各种绘图需求
因为matplotlib.pyplot
模块内部就有个默认的 Figure 对象
当使用此模块的相关方法绘图时,底层实际调用了默认 Figure 对象的相关方法
绘制线形图的例子:
In [58]: import matplotlib.pyplot as plt
In [59]: s = Series(np.random.randn(55), index=range(0, 550, 10))
# Series 的 plot 方法可以将数据注入默认子图,生成线形图
# index 作为 x 轴的值, 元素值作为 y 轴的值
In [60]: s.plot()
Out[60]: <matplotlib.axes._subplots.AxesSubplot at 0x7f076fed8c18>
In [61]: plt.show() # 图就不展示了
绘制箱线图的例子:
In [86]: import matplotlib.pyplot as plt
In [87]: df = DataFrame(np.random.rand(5, 4), columns=list('ABCD'))
# 每列数据生成一个箱线图,columns 的值作为 x 轴的标签
In [88]: df.boxplot()
Out[88]: <matplotlib.axes._subplots.AxesSubplot at 0x7f076fdeb780>
In [89]: plt.show() # 见下图
DataFrame 绘制箱线图
网友评论