**Matplotlib的figure对象和subplot对象**1
Matplotlib
Matplotlib:是一个用于创建出版质量图表的桌面绘图包
import matplotlib.pyplot as plt
pyplot模块包含Matplotlib API函数
figure
Matplotlib的图像均位于figure对象中
创建figure
plt.figure()
#这种写法会弹出一个空窗口,因此不能通过空的figure绘图,必须用add_subplot创建一个或多个subplot才行
ax1 = fig.add_subplot(2, 2, 1) #意思是图像应该是2x2 的,且当前选中的是4个subplot中的第一个
subplot
直方图 hist
散点图 scatter
柱状图 bar
矩阵绘图 plt.imshow()
混淆矩阵,三个维度的关系
#引入Matplotlib包
import matplotlib.pyplot as plt
%matplotlib inline
#创建figure
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
#在subplot上作图
import numpy as np
random_arr = np.random.randn(100)
#默认是在左后一次使用subplot的位置上作图,但是在jupyter里无效
plt.plot(random_arr)
plt.show()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
图形中文显示
Matplotlib 默认情况不支持中文,我们可以使用以下简单的方法来解决:
#plt.title('日平均pm2.5')
ax2.set_title('日平均pm2.5')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False1
2
3
4
5
也可以用此方法设置中文
SimHei.ttf 文件放在当前执行的代码文件中:
实例
import numpy as np
from matplotlib import pyplot as plt
import matplotlib1
2
3
4
fname 为 你下载的字体库路径,注意 SimHei.ttf 字体的路径
zhfont1 = matplotlib.font_manager.FontProperties(fname="SimHei.ttf")
x = np.arange(1,11)
y = 2 * x + 5
plt.title("测试", fontproperties=zhfont1)1
2
3
4
5
6
fontproperties 设置中文显示,fontsize 设置字体大小
plt.xlabel("x 轴", fontproperties=zhfont1)
plt.ylabel("y 轴", fontproperties=zhfont1)
plt.plot(x,y)
plt.show()1
2
3
4
在subplot上绘制图形的方法
绘制直方图
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.linspace(-5, 15, 50)
plt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
plt.show()1
2
3
4
5
6
7
绘制散点图
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.arange(50)
y = x + 5 * np.random.rand(50)
plt.scatter(x, y)
plt.show()1
2
3
4
5
6
7
绘制柱状图
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax = plt.subplot(1,1,1)
ax.bar(x, y1, width, color='r')
ax.bar(x+width, y2, width, color='g')
ax.set_xticks(x+width)
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
plt.show()1
2
3
4
5
6
7
8
9
10
11
12
矩阵绘图
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
m = np.random.rand(10,10)
print(m)
plt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean)
plt.colorbar()
plt.show()1
2
3
4
5
6
7
8
9
用plt以及subplot绘图
fig, ax = plt.subplots(1)
ax.plot(np.random.randn(1000).cumsum(), label='line0')
# 设置刻度
#plt.xlim([0,500])
ax.set_xlim([0, 800])
# 设置显示的刻度
#plt.xticks([0,500])
ax.set_xticks(range(0,500,100))
# 设置刻度标签
ax.set_yticklabels(['Jan', 'Feb', 'Mar'])
# 设置坐标轴标签
ax.set_xlabel('Number')
ax.set_ylabel('Month')
# 设置标题
ax.set_title('Example')
# 图例
ax.plot(np.random.randn(1000).cumsum(), label='line1')
ax.plot(np.random.randn(1000).cumsum(), label='line2')
ax.legend()
ax.legend(loc='best')
plt.legend()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
注意: 在写代码的时候要么就用plt库要么就用subplot库,不能混用,不然会把报错