Matplotlib 如何使用Matplotlib在同一个窗口中绘制多张图
在Python中,Matplotlib是一个广泛使用的绘图库,它可以绘制各种类型的图形。在本文中,我们将讲解如何使用Matplotlib在同一个窗口中绘制多张图。
阅读更多:Matplotlib 教程
创建多图
使用Matplotlib创建多张图是非常简单的。我们只需要在调用绘图函数之前调用plt.subplots()
函数即可。下面是一个简单的例子,展示了如何在同一个窗口中绘制多张图:
import matplotlib.pyplot as plt
import numpy as np
# Create some data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)
# Plot the data
ax1.plot(x, y1)
ax2.plot(x, y2)
# Show the plot
plt.show()
在这个例子中,我们先创建了一些数据,并且使用plt.subplots()
创建了两个子图。plt.subplots(1, 2)
表示我们要创建一个1行2列的窗口,并在其中放置两个子图(ax1和ax2)。接下来,我们在每个子图上绘制了不同的曲线,并使用plt.show()
显示了窗口。
多个子图和坐标轴
除了2个子图的情况,我们还可以在一个窗口中放置多个子图。下面是一个简单的例子,展示了如何在同一个窗口中绘制4张图:
import matplotlib.pyplot as plt
import numpy as np
# Create some data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = x ** 2
# Create four subplots
fig, axes = plt.subplots(nrows=2, ncols=2)
# Plot the data
axes[0, 0].plot(x, y1)
axes[0, 1].plot(x, y2)
axes[1, 0].plot(x, y3)
axes[1, 1].plot(x, y4)
# Add some labels
axes[0, 0].set_title('sin(x)')
axes[0, 1].set_title('cos(x)')
axes[1, 0].set_title('tan(x)')
axes[1, 1].set_title('x^2')
axes[0, 0].set_xlabel('x')
axes[0, 1].set_xlabel('x')
axes[1, 0].set_xlabel('x')
axes[1, 1].set_xlabel('x')
axes[0, 0].set_ylabel('y')
axes[0, 1].set_ylabel('y')
axes[1, 0].set_ylabel('y')
axes[1, 1].set_ylabel('y')
# Show the plot
plt.show()
在这个例子中,我们使用plt.subplots(nrows=2, ncols=2)
创建了一个2行2列的窗口,并在其中放置了4个子图。我们可以通过axes[row_index, column_index]
来访问每个子图。接下来,我们在每个子图上绘制不同的曲线,并添加了一些标题,标签和标签。
共享轴
如果我们想在多个子图中共享坐标轴,可以使用sharex
和sharey
参数。例如,下面是一个展示了如何共享x轴的例子:
import matplotlib.pyplot as plt
import numpy as np
# Create some data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = x ** 2
# Create four subplots
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True)
# Plot the data
axes[0, 0].plot(x, y1)
axes[0, 1].plot(x, y2)
axes[1, 0].plot(x, y3)
axes[1, 1].plot(x, y4)
# Add some labels
axes[0, 0].set_title('sin(x)')
axes[0, 1].set_title('cos(x)')
axes[1, 0].set_title('tan(x)')
axes[1, 1].set_title('x^2')
# Set shared x label
fig.text(0.5, 0.04, 'x', ha='center')
# Show the plot
plt.show()
在这个例子中,我们使用sharex=True
来共享所有子图的x轴。然后我们在每个子图上绘制了不同的曲线,并添加了一些标题。我们使用了fig.text()
函数来为整个窗口添加一个共享x轴的标签。
子图网格
除了使用nrows
和ncols
参数来创建子图网格之外,我们还可以使用gridspec
来控制子图的大小和位置。下面是一个展示了如何使用gridspec
来创建一个自定义子图网格的例子:
import matplotlib.pyplot as plt
import numpy as np
# Create some data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = x ** 2
# Create a custom grid
gs = plt.GridSpec(2, 2, width_ratios=[1, 2], height_ratios=[2, 1])
gs.update(wspace=0.4, hspace=0.4)
# Create four subplots
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[0, 1])
ax3 = plt.subplot(gs[1, :])
# Plot the data
ax1.plot(x, y1)
ax2.plot(x, y2)
ax3.plot(x, y3)
# Add some labels
ax1.set_title('sin(x)')
ax2.set_title('cos(x)')
ax3.set_title('tan(x)')
ax1.set_xlabel('x')
ax2.set_xlabel('x')
ax3.set_xlabel('x')
ax1.set_ylabel('y')
ax2.set_ylabel('y')
ax3.set_ylabel('y')
# Show the plot
plt.show()
在这个例子中,我们首先使用plt.GridSpec()
函数创建了一个自定义网格。我们可以通过width_ratios
和height_ratios
参数来指定每行和每列的宽度/高度。然后,我们在网格中创建了三个子图,并在每个子图上绘制了不同的曲线。我们使用了gs[0, 0]
,gs[0, 1]
和gs[1, :]
来获取每个子图的位置。最后,我们添加了一些标题,标签和标签,并使用plt.show()
显示了整个窗口。
总结
在本文中,我们学习了如何使用Matplotlib在同一个窗口中绘制多张图。我们介绍了如何创建多个子图和坐标轴,共享轴以及如何使用gridspec
来创建自定义子图网格。这些技术可以帮助我们更好地组织和展示数据,并为我们的研究/分析提供更好的可视化结果。