Matplotlib (https://matplotlib.org/) 是一个基于 NumPy 的二维绘图库。下面是一些基本示例。 更多示例可参见官方文档(https://matplotlib.org/2.0.2/gallery.html 和 https://matplotlib.org/2.0.2/examples/index.html)
Section 65.1: 具有相同 X 轴但不同 Y 轴的曲线图 : 使用 twinx()
在本例中,我们将在同一幅图中绘制一条正弦曲线和一条双曲正弦曲线,它们的 “x 轴 ”相同,而 “y 轴 ”不同。这可以通过使用 twinx() 命令来实现。
# Python 中的绘图教程
# 通过双 x 轴添加多个绘图
# 适合具有不同 y 轴范围的绘图
# 分离坐标轴和图形对象
# 复制坐标轴对象和曲线图
# 使用坐标轴设置属性
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)
# 将图形对象和坐标轴对象
# 与绘图对象分开
fig, ax1 = plt.subplots()
# 用不同的 y 轴和相同的 x 轴复制坐标轴
# 用相同的 x 轴
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis
# 在轴 1 和轴 2 上绘制曲线,并获取曲线手柄
curve1, = ax1.plot(x, y, label="sin", color='r')
curve2, = ax2.plot(x, z, label="sinh", color='b')
# 制作曲线列表,以访问曲线中的参数
curves = [curve1, curve2]
# 通过轴 1 或轴 2 对象添加图例。
# 通常一条命令就足够了
# ax1.legend() # 不会显示 ax2 的图例
# ax2.legend() # 不会显示 ax1 的图例
ax1.legend(curves, [curve.get_label() for curve in curves])
# ax2.legend(curves, [curve.get_label() for curve in curves]) # 也有效
# 全局数字属性
plt.title("Plot of sine and hyperbolic sine")
plt.show()

Section 65.2: 使用 twiny() 绘制具有共同 Y 轴和不同 X 轴的曲线图
在本示例中,使用 twiny() 方法演示了具有相同 y 轴但不同 x 轴的曲线图。此外,还为曲线图添加了一些附加功能,如标题、图例、标签、网格、轴刻度和颜色。
# Python 中的绘图教程
# 通过双 y 轴添加多个绘图
# 适合具有不同 x 轴范围的绘图
# 分离坐标轴和图形对象
# 复制坐标轴对象和曲线图
# 使用坐标轴设置属性
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(0, 2.0*np.pi, 101)
x1 = np.sin(y)
x2 = np.sinh(y)
# 在 x 轴和 y 轴上打钩的数值
ynumbers = np.linspace(0, 7, 15)
xnumbers1 = np.linspace(-1, 1, 11)
xnumbers2 = np.linspace(0, 300, 7)
# 将图形对象和坐标轴对象
# 与绘图对象分开
fig, ax1 = plt.subplots()
# 用不同的 x 轴和相同的 y 轴复制坐标轴
# 用相同的 y 轴
ax2 = ax1.twiny() # ax2 和 ax1 将有共同的 y 轴和不同的 x 轴
# 在坐标轴 1 和 2 上绘制曲线,并获取坐标轴手柄
curve1, = ax1.plot(x1, y, label="sin", color='r')
curve2, = ax2.plot(x2, y, label="sinh", color='b')
# 制作曲线列表,以访问曲线中的参数
curves = [curve1, curve2]
# 通过轴 1 或轴 2 对象添加图例。
# 通常一条命令就足够了
# ax1.legend() # 不会显示 ax2 的图例
# ax2.legend() # 不会显示 ax1 的图例
# ax1.legend(curves, [curve.get_label() for curve in curves])
ax2.legend(curves, [curve.get_label() for curve in curves]) # 也有效
# 通过坐标轴显示 x 轴标签
ax1.set_xlabel("Magnitude", color=curve1.get_color())
ax2.set_xlabel("Magnitude", color=curve2.get_color())
# y axis label via the axes
ax1.set_ylabel("Angle/Value", color=curve1.get_color())
# ax2.set_ylabel(“Magnitude”, color=curve2.get_color()) # 不起作用
# ax2 对 Y 轴没有属性控制
# y ticks - make them coloured as well
ax1.tick_params(axis='y', colors=curve1.get_color())
# ax2.tick_params(axis='y', colors=curve2.get_color()) # 不起作用
# ax2 has no property control over y axis
# 通过坐标轴进行 x 轴刻度
ax1.tick_params(axis='x', colors=curve1.get_color())
ax2.tick_params(axis='x', colors=curve2.get_color())
# 设置 x 刻度
ax1.set_xticks(xnumbers1)
ax2.set_xticks(xnumbers2)
# 设置 y 刻度
ax1.set_yticks(ynumbers)
# ax2.set_yticks(ynumbers) # 同样有效
# 通过轴 1 绘制网格 # 如果轴 1 用于
# 定义公共 x 轴的属性
# ax1.grid(color=curve1.get_color())
# 使用轴 2 绘制网格
ax1.grid(color=curve2.get_color())
ax2.grid(color=curve2.get_color())
ax1.xaxis.grid(False)
# 全局数字属性
plt.title("Plot of sine and hyperbolic sine")
plt.show()

Section 65.3: Matplotlib 中的简单绘图
本例说明如何使用 Matplotlib 创建一条简单的正弦曲线
# Python 绘图教程
# 启动简单绘图
import numpy as np
import matplotlib.pyplot as plt
# 角度在 0 和 2*pi 之间变化
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x) # sine function
plt.plot(x, y)
plt.show()

Section 65.4: 为简单绘图添加更多功能:轴标签、标题、轴刻度、网格和图例
在本示例中,我们将为正弦曲线图添加更多特征,即标题、坐标轴标签、标题、坐标轴刻度、网格和图例。
# Python 绘图教程
# 增强绘图
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
# 在 x 轴和 y 轴上做刻度的值
xnumbers = np.linspace(0, 7, 15)
ynumbers = np.linspace(-1, 1, 11)
plt.plot(x, y, color='r', label='sin') # r - red colour
plt.xlabel("Angle in Radians")
plt.ylabel("Magnitude")
plt.title("Plot of some trigonometric functions")
plt.xticks(xnumbers)
plt.yticks(ynumbers)
plt.legend()
plt.grid()
plt.axis([0, 6.5, -1.1, 1.1]) # [xstart, xend, ystart, yend]
plt.show()

Section 65.5: 与 MATLAB 类似,通过叠加在同一图表中绘制多个图表
在此示例中,正弦曲线和余弦曲线通过叠加绘制在同一图表中。
# Python 中的绘图教程
# 通过叠加添加多个绘图
# 适合共享相似 x、y 极限的绘图
# 使用单一绘图命令和图例
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.cos(x)
# 在 x 轴和 y 轴上做刻度的值
xnumbers = np.linspace(0, 7, 15)
ynumbers = np.linspace(-1, 1, 11)
plt.plot(x, y, 'r', x, z, 'g') # r, g - red, green colour
plt.xlabel("Angle in Radians")
plt.ylabel("Magnitude")
plt.title("Plot of some trigonometric functions")
plt.xticks(xnumbers)
plt.yticks(ynumbers)
plt.legend(['sine', 'cosine'])
plt.grid()
plt.axis([0, 6.5, -1.1, 1.1]) # [xstart, xend, ystart, yend]
plt.show()

Section 65.6: 使用绘图叠加和单独绘图命令在同一图表中绘制多个绘图
与前面的示例类似,这里使用不同的绘图命令在同一图形上绘制正弦曲线和余弦曲 线。这更符合毕达哥拉斯原理,可用于为每个曲线图获取单独的句柄。
# Plotting tutorials in Python
# Adding Multiple plots by superimposition
# Good for plots sharing similar x, y limits
# Using multiple plot commands
# Much better and preferred than previous
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.cos(x)
# values for making ticks in x and y axis
xnumbers = np.linspace(0, 7, 15)
ynumbers = np.linspace(-1, 1, 11)
plt.plot(x, y, color='r', label='sin') # r - red colour
plt.plot(x, z, color='g', label='cos') # g - green colour
plt.xlabel("Angle in Radians")
plt.ylabel("Magnitude")
plt.title("Plot of some trigonometric functions")
plt.xticks(xnumbers)
plt.yticks(ynumbers)
plt.legend()
plt.grid()
plt.axis([0, 6.5, -1.1, 1.1]) # [xstart, xend, ystart, yend]
plt.show()
