2.Matplotlib
Matplotlib 是一个 Python 的 2D绘图库,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。 官方网站http://matplotlib.org
- 用于创建出版质量图表的绘图工具库
- 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建
- 目的是为Python构建一个Matlab式的绘图接口
- pyploy模块包含了常用的matplotlib API函
# 导入相关模块
import matplotlib.pyplot as plt
import numpy as np
画一个简单的图形
这里我们通过画出一个正弦曲线图来讲解下基本用法。
首先通过 np.linspace
方式生成 x,它包含了 50 个元素的数组,这 50 个元素均匀的分布在 [0, 2pi] 的区间上。然后通过 np.sin(x)
生成 y。
x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)
有了 x 和 y 数据之后,我们通过 plt.plot(x, y)
来画出图形,并通过 plt.show()
来显示。
plt.plot(x, y)
plt.show()
在一张图纸里绘制多个图形
有时候,可能需要在一个图纸里绘制多个图形,这里我们同时绘制了 (x, y), (x, y * 2)两个图形。
plt.plot(x, y)
plt.plot(x, y * 2)
plt.show()
绘制出图形之后,我们可以自己调整更多的样式,比如颜色、点、线。
plt.plot(x, y, 'y*-')
plt.plot(x, y * 2, 'm--')
plt.show()
可以看到,设置样式时,就是增加了一个字符串参数,比如 'y*-'
,其中 y 表示黄色,* 表示 星标的点,- 表示实线。
这里列举一些常见的颜色表示方式:
颜色 | 表示方式 |
---|---|
蓝色 | b |
绿色 | g |
红色 | r |
青色 | c |
品红 | m |
黄色 | y |
黑色 | k |
白色 | w |
常见的点的表示方式:
点的类型 | 表示方式 |
---|---|
点 | . |
像素 | , |
圆 | o |
方形 | s |
三角形 | ^ |
常见的线的表示方式:
线的类型 | 表示方式 |
---|---|
直线 | - |
虚线 | -- |
点线 | : |
点划线 | -. |
更多设置
Matplotlib 支持各种灵活的设置,这里我们列举一些常见的内容。
设置 figure
你可以认为Matplotlib绘制的图形都在一个默认的 figure 中,当然了,你可以自己创建 figure,好处就是可以控制更多的参数,常见的就是控制图形的大小,这里创建一个 figure,设置大小为 (6, 3)
。
plt.figure(figsize=(6, 3))
plt.plot(x, y)
plt.plot(x, y * 2)
plt.show()
设置标题
来看下如何设置标题。
plt.plot(x, y)
plt.plot(x, y * 2)
plt.title("sin(x) & 2sin(x)")
plt.show()
直接通过 plt.title
即可设置图形标题。
设置坐标轴
来看下如何设置坐标轴的范围以及名称。
plt.plot(x, y)
plt.plot(x, y * 2)
plt.xlim((0, np.pi + 1))
plt.ylim((-3, 3))
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
通过 xlim
和 ylim
来设限定轴的范围,通过 xlabel
和 ylabel
来设置轴的名称。
此外,我们也可以通过 xticks
和 yticks
来设置轴的刻度。
plt.plot(x, y)
plt.plot(x, y * 2)
plt.xticks((0, np.pi * 0.5, np.pi, np.pi * 1.5, np.pi * 2))
plt.show()
设置 label 和 legend
设置 label 和 legend 的目的就是为了区分出每个数据对应的图形名称。
plt.plot(x, y, label="sin(x)")
plt.plot(x, y * 2, label="2sin(x)")
# plt.legend()
plt.legend(loc='best')
plt.show()
添加注释
有时候我们需要对特定的点进行标注,我们可以使用 plt.annotate
函数来实现。
这里我们要标注的点是 (x0, y0) = (π, 0)
。
我们也可以使用 plt.text
函数来添加注释。
plt.plot(x, y)
x0 = np.pi
y0 = 0
# 画出标注点
plt.scatter(x0, y0, s=50)
plt.annotate('sin(np.pi)=%s' % y0, xy=(np.pi, 0), xycoords='data', xytext=(+30, -30),
textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2"))
plt.text(0.5, -0.25, "sin(np.pi) = 0", fontdict={'size': 16, 'color': 'r'})
plt.show()
对于 annotate
函数的参数,做一个简单解释:
'sin(np.pi)=%s' % y0
代表标注的内容,可以通过字符串 %s 将 y0 的值传入字符串;- 参数
xycoords='data'
是说基于数据的值来选位置; xytext=(+30, -30)
和textcoords='offset points'
表示对于标注位置的描述 和 xy 偏差值,即标注位置是 xy 位置向右移动 30,向下移动30;arrowprops
是对图中箭头类型和箭头弧度的设置,需要用 dict 形式传入。
使用子图
有时候我们需要将多张子图展示在一起,可以使用 subplot()
实现。即在调用 plot()
函数之前需要先调用 subplot()
函数。该函数的第一个参数代表子图的总行数,第二个参数代表子图的总列数,第三个参数代表活跃区域。
ax1 = plt.subplot(2, 2, 1) # (行,列,活跃区)
plt.plot(x, np.sin(x), 'r')
ax2 = plt.subplot(2, 2, 2, sharey=ax1) # 与 ax1 共享y轴
plt.plot(x, 2 * np.sin(x), 'g')
ax3 = plt.subplot(2, 2, 3)
plt.plot(x, np.cos(x), 'b')
ax4 = plt.subplot(2, 2, 4, sharey=ax3) # 与 ax3 共享y轴
plt.plot(x, 2 * np.cos(x), 'y')
plt.show()
上面的 subplot(2, 2, x)
表示将图像窗口分为 2 行 2 列。x 表示当前子图所在的活跃区。
可以看到,上面的每个子图的大小都是一样的。有时候我们需要不同大小的子图。比如将上面第一张子图完全放置在第一行,其他的子图都放在第二行。
ax1 = plt.subplot(2, 1, 1) # (行,列,活跃区)
plt.plot(x, np.sin(x), 'r')
ax2 = plt.subplot(2, 3, 4)
plt.plot(x, 2 * np.sin(x), 'g')
ax3 = plt.subplot(2, 3, 5, sharey=ax2)
plt.plot(x, np.cos(x), 'b')
ax4 = plt.subplot(2, 3, 6, sharey=ax2)
plt.plot(x, 2 * np.cos(x), 'y')
plt.show()
简单解释下,plt.subplot(2, 1, 1)
将图像窗口分为了 2 行 1 列, 当前活跃区为 1。
使用 plt.subplot(2, 3, 4)
将整个图像窗口分为 2 行 3 列, 当前活跃区为 4。
解释下为什么活跃区为 4,因为上一步中使用 plt.subplot(2, 1, 1)
将整个图像窗口分为 2 行 1 列, 第1个小图占用了第1个位置, 也就是整个第1行. 这一步中使用 plt.subplot(2, 3, 4)
将整个图像窗口分为 2 行 3 列, 于是整个图像窗口的第1行就变成了3列, 也就是成了3个位置, 于是第2行的第1个位置是整个图像窗口的第4个位置。
常见的图形
这里带大家画一些常见的示例图。
散点图
首先来看下如何绘制散点图。
k = 500
x = np.random.rand(k)
y = np.random.rand(k)
size = np.random.rand(k) * 50 # 生成每个点的大小
colour = np.arctan2(x, y) # 生成每个点的颜色大小
plt.scatter(x, y, s=size, c=colour)
plt.colorbar() # 添加颜色栏
plt.show()
上面我们首先生成了要绘制的数据的点x
和 y
,接下来为每个数据点生成控制大小的数组 size
,然后未每个数据点生成控制颜色的数组 colour
。最后通过 colorbar()
来增加一个颜色栏。
柱状图
柱状图我们经常会用到,我们来看下如何画出柱状图,并在图上标注出数据对应的数值。
k = 10
x = np.arange(k)
y = np.random.rand(k)
plt.bar(x, y) # 画出 x 和 y 的柱状图
# 增加数值
for x, y in zip(x, y):
plt.text(x, y , '%.2f' % y, ha='center', va='bottom')
plt.show()
生成数据 x
和 y
之后,调用 plt.bar
函数绘制出柱状图,然后通过 plt.text
标注数值,设置参数 ha='center'
横向居中对齐,设置 va='bottom'
纵向底部(顶部)对齐。
中文乱码解决
Matplotlib 有个让人恼火的问题是,默认情况下,Matplotlib 中文会乱码。
x = ['北京', '上海', '深圳', '广州']
y = [60000, 58000, 50000, 52000]
plt.plot(x, y)
plt.show()
可以看到,上面所有的中文都乱码了,显示成方框了,如何解决呢?
其实只需要配置下后台字体即可。
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.plot(x, y)
plt.show()
数据加载和简单的数据探索
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn import datasets
数据的获取
# 加载数据集load_xxx
iris = datasets.load_iris()
# 是一个字典
iris.keys()
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names'])
# 数据文档
print(iris.DESCR)
Iris Plants Database
====================
Notes
-----
Data Set Characteristics:
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica
:Summary Statistics:
============== ==== ==== ======= ===== ====================
Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================
:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
:Date: July, 1988
This is a copy of UCI ML iris datasets.
http://archive.ics.uci.edu/ml/datasets/Iris
The famous Iris database, first used by Sir R.A Fisher
This is perhaps the best known database to be found in the
pattern recognition literature. Fisher's paper is a classic in the field and
is referenced frequently to this day. (See Duda & Hart, for example.) The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant. One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.
References
----------
- Fisher,R.A. "The use of multiple measurements in taxonomic problems"
Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
Mathematical Statistics" (John Wiley, NY, 1950).
- Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.
(Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.
- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
Structure and Classification Rule for Recognition in Partially Exposed
Environments". IEEE Transactions on Pattern Analysis and Machine
Intelligence, Vol. PAMI-2, No. 1, 67-71.
- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions
on Information Theory, May 1972, 431-433.
- See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II
conceptual clustering system finds 3 classes in the data.
- Many, many more ...
# 数据集
iris.data
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.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1.8],
[6.5, 3. , 5.8, 2.2],
[7.6, 3. , 6.6, 2.1],
[4.9, 2.5, 4.5, 1.7],
[7.3, 2.9, 6.3, 1.8],
[6.7, 2.5, 5.8, 1.8],
[7.2, 3.6, 6.1, 2.5],
[6.5, 3.2, 5.1, 2. ],
[6.4, 2.7, 5.3, 1.9],
[6.8, 3. , 5.5, 2.1],
[5.7, 2.5, 5. , 2. ],
[5.8, 2.8, 5.1, 2.4],
[6.4, 3.2, 5.3, 2.3],
[6.5, 3. , 5.5, 1.8],
[7.7, 3.8, 6.7, 2.2],
[7.7, 2.6, 6.9, 2.3],
[6. , 2.2, 5. , 1.5],
[6.9, 3.2, 5.7, 2.3],
[5.6, 2.8, 4.9, 2. ],
[7.7, 2.8, 6.7, 2. ],
[6.3, 2.7, 4.9, 1.8],
[6.7, 3.3, 5.7, 2.1],
[7.2, 3.2, 6. , 1.8],
[6.2, 2.8, 4.8, 1.8],
[6.1, 3. , 4.9, 1.8],
[6.4, 2.8, 5.6, 2.1],
[7.2, 3. , 5.8, 1.6],
[7.4, 2.8, 6.1, 1.9],
[7.9, 3.8, 6.4, 2. ],
[6.4, 2.8, 5.6, 2.2],
[6.3, 2.8, 5.1, 1.5],
[6.1, 2.6, 5.6, 1.4],
[7.7, 3. , 6.1, 2.3],
[6.3, 3.4, 5.6, 2.4],
[6.4, 3.1, 5.5, 1.8],
[6. , 3. , 4.8, 1.8],
[6.9, 3.1, 5.4, 2.1],
[6.7, 3.1, 5.6, 2.4],
[6.9, 3.1, 5.1, 2.3],
[5.8, 2.7, 5.1, 1.9],
[6.8, 3.2, 5.9, 2.3],
[6.7, 3.3, 5.7, 2.5],
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]])
# 形状
iris.data.shape
(150, 4)
# 每一列的意思
iris.feature_names
['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
# 每一个样本(鸢尾花)对应的类型
iris.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
iris.target.shape
(150,)
# 0,1,2对应的名字
iris.target_names
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
数据的可视化
# 所有的行,取前两列
X = iris.data[:,:2]
X
# X[:,0]这样可以取出第0列
plt.scatter(X[:,0],X[:,1])
<matplotlib.collections.PathCollection at 0x1a17c22748>
# 使用不同的颜色标识不同类别的鸢尾花
y = iris.target
y
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
# marker 是形状
for i,colors,marker in [(0,"red","o"),(1,"blue","+"),(2,"green","x")]:
plt.scatter(X[y==i,0],X[y==i,1],color=colors,marker=marker)
# 使用另外两个维度
X = iris.data[:,2:]
# marker 是形状
for i,colors,marker in [(0,"red","o"),(1,"blue","+"),(2,"green","x")]:
plt.scatter(X[y==i,0],X[y==i,1],color=colors,marker=marker)
