温馨提示×

Matplotlib中如何在一个图形窗口中创建多个子图

小亿
93
2024-05-20 12:30:26
栏目: 编程语言

在Matplotlib中可以使用subplot()方法在一个图形窗口中创建多个子图。该方法接受三个整数参数,分别代表子图的行数、列数和当前子图的索引。例如,如果要在一个2x2的图形窗口中创建四个子图,可以按照如下步骤操作:

import matplotlib.pyplot as plt

# 创建第一个子图
plt.subplot(2, 2, 1)
plt.plot([1, 2, 3, 4])

# 创建第二个子图
plt.subplot(2, 2, 2)
plt.plot([4, 3, 2, 1])

# 创建第三个子图
plt.subplot(2, 2, 3)
plt.plot([1, 2, 3, 4])

# 创建第四个子图
plt.subplot(2, 2, 4)
plt.plot([4, 3, 2, 1])

plt.show()

在上面的例子中,subplot()方法分别创建了一个2x2的图形窗口和四个子图,并使用plot()方法在每个子图中绘制了一条曲线。最后通过show()方法显示图形。

0