直接看几段代码即可:
# 加载模块的方式import matplotlib.pyplot as pltimport numpy as np# 最简单的单线图x = np.linspace(0, 2 * np.pi, 50)plt.plot(x, np.sin(x)) # 如果没有第一个参数 x,图形的 x 坐标默认为数组的索引plt.show() # 显示图形# 两条线x = np.linspace(0, 2 * np.pi, 50)plt.plot(x, np.sin(x), x, np.sin(2 * x))plt.show()# 自定义曲线的外观x = np.linspace(0, 2 * np.pi, 50)plt.plot(x, np.sin(x), 'r-o', x, np.cos(x), 'g--')plt.show()# 使用子图x = np.linspace(0, 2 * np.pi, 50)plt.subplot(2, 1, 1) # (行,列,活跃区)plt.plot(x, np.sin(x), 'r')plt.subplot(2, 1, 2)plt.plot(x, np.cos(x), 'g')plt.show()# 直方图x = np.random.randn(1000)plt.hist(x, 50)plt.show()# 添加标题,坐标轴标记和图例x = np.linspace(0, 2 * np.pi, 50)plt.plot(x, np.sin(x), 'r-x', label='Sin(x)')plt.plot(x, np.cos(x), 'g-^', label='Cos(x)')plt.legend() # 展示图例plt.xlabel('Rads') # 给 x 轴添加标签plt.ylabel('Amplitude') # 给 y 轴添加标签plt.title('Sin and Cos Waves') # 添加图形标题plt.show()