1、matplotlib简介
Python 的 2D绘图库,为Python构建一个Matlab式的绘图接口,通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等
2、matplotlib的使用
(1)折线图
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
print(type(fig))
<class 'matplotlib.figure.Figure'>
arr = np.random.randn(100)
plt.plot(arr)
plt.show()
Subplot 可以通过add_subplot来分割figure,表示可以在figure的不同位置上作图 fig.
add_subplot(a, b, c) a,b 表示将fig分割成 a*b 的区域 c 表示当前选中要操作的区域,
注意:从1开始编号(不是从0开始)
plot 绘图的区域是最后一次指定subplot的位置 (jupyter notebook里不能正确显示)
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)
ax1.plot(np.random.randn(100))
ax2.plot(np.random.randn(100))
ax3.plot(np.random.randn(100))
ax4.plot(np.random.randn(100))
plt.show()