如何将几个matplotlib axes subplot组合成一个图形?
要将几个matplotlib axes subplot组合成一个图形,我们可以使用 subplots() 方法和 nrow=2 。
步骤
- 设置图形大小并调整子图之间和周围的填充。
- 使用numpy创建 x 、 y1 和 y2 数据点。
- 创建图形和一组子图。
- 使用 plot() 方法绘制 x,y1 和 y2 数据点。
- 使用 show() 方法显示图形。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-10, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
fig, axes = plt.subplots(nrows=2)
line1, = axes[0].plot(x, y1, color='red')
line2, = axes[1].plot(x, y2, color='green')
plt.show()