Matplotlib 使用subplots()和GridSpec()
阅读更多:Matplotlib 教程
引言
Matplotlib是一个常用的数据可视化库,可以用它来进行各种图形绘制。本文将重点介绍Matplotlib中的两个重要函数:subplots()
和GridSpec()
。使用这两个函数可以帮助我们更方便地进行子图的排布和绘制。
subplots()函数
可以使用Matplotlib中的subplots()
函数来创建一个包含多个子图的图形对象。该函数的用法如下:
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))
此时,fig
为最终的图形对象,axs
为一个包含所有子图的矩阵。参数nrows
和ncols
分别指定了子图矩阵的行数和列数。
接下来,我们可以通过下标的方式访问每个子图,并对其进行绘制。例如:
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
axs[0, 1].scatter([1, 2, 3], [4, 5, 6])
axs[1, 0].hist([1, 2, 3, 4, 5, 6], bins=3)
axs[1, 1].boxplot([1, 2, 3, 4, 5, 6])
这样,我们就可以在一个图形对象中绘制多个子图了。
GridSpec()函数
如果我们需要更灵活地进行子图的排布,那么可以使用Matplotlib中的GridSpec()
函数。该函数的用法如下:
gs = plt.GridSpec(nrows=2, ncols=2, figure=fig)
此时,gs
为一个包含所有子图位置的网格对象。与subplots()
函数类似,参数nrows
和ncols
分别指定了子图网格的行数和列数,参数figure
指定了所要使用的图形对象。
接下来,我们可以通过gs
对象来获取每个子图的位置,例如:
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, :])
在上面的代码中,add_subplot()
函数可以将一个子图添加到指定的位置。其中,:
表示跨越整行或整列。
类似于subplots()
函数,我们可以在每个子图中进行绘制操作,例如:
ax1.plot([1, 2, 3], [4, 5, 6])
ax2.scatter([1, 2, 3], [4, 5, 6])
ax3.hist([1, 2, 3, 4, 5, 6], bins=3)
这样,我们就可以在一个图形对象中更灵活地排布子图了。
示例说明
下面通过几个示例来说明subplots()
和GridSpec()
函数的使用。
示例1
首先,我们先使用subplots()
函数来创建一个包含2×3个子图的图形对象。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(12, 6))
接下来,在第一个子图中绘制一条线,第二个子图中绘制一个点,第三个子图中绘制一个条形图,第四个子图中绘制一个箱形图,第五个子图中绘制一个饼图,第六个子图不进行绘制。
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
axs[0, 1].scatter([1, 2, 3], [4, 5, 6])
axs[0, 2].bar([1,2, 3], [4, 5, 6])
axs[1, 0].boxplot([1, 2, 3, 4, 5, 6])
axs[1, 1].pie([1, 2, 3])
可以看到,使用subplots()
函数可以方便地创建一个多个子图的图形对象,并且可以通过下标的方式访问每个子图。
示例2
接下来,我们使用GridSpec()
函数来创建一个包含3个子图的图形对象。其中,第一个子图占据整个第一行,第二个子图占据整个第二列,第三个子图占据整个第二行和第三列。
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 6))
gs = plt.GridSpec(nrows=3, ncols=3, figure=fig)
ax1 = fig.add_subplot(gs[0, :])
ax1.plot([1, 2, 3], [4, 5, 6])
ax2 = fig.add_subplot(gs[1, 2])
ax2.scatter([1, 2, 3], [4, 5, 6])
ax3 = fig.add_subplot(gs[1:, 0:2])
ax3.hist([1, 2, 3, 4, 5, 6], bins=3)
可以看到,使用GridSpec()
函数可以更灵活地控制每个子图的位置和大小。
示例3
最后,我们使用subplots()
函数和GridSpec()
函数来创建一个包含3个子图的图形对象。
import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(12, 8))
gs = axs[1, 1].get_gridspec()
ax1 = fig.add_subplot(gs[0, 0:2])
ax1.plot([1, 2, 3], [4, 5, 6])
ax2 = fig.add_subplot(gs[0, 2])
ax2.scatter([1, 2, 3], [4, 5, 6])
ax3 = fig.add_subplot(gs[1, 0])
ax3.hist(np.random.randn(1000))
ax4 = fig.add_subplot(gs[1, 1])
ax4.boxplot([1, 2, 3, 4, 5, 6])
ax5 = axs[0, 2]
ax6 = axs[1, 2]
fig.delaxes(axs[0, 1])
ax5.pie([1, 2, 3])
ax6.bar([1, 2, 3], [4, 5, 6])
在上面的代码中,我们首先使用subplots()
函数创建一个包含2×3个子图的图形对象。然后,我们使用get_gridspec()
方法获取了第2行第2列子图所在的网格对象,进而使用add_subplot()
函数向该网格对象中添加了两个子图。最后,我们用pie()
函数和bar()
函数分别在第0行第2列和第1行第2列添加了两个子图,并使用delaxes()
函数删除了原来的第0行第1列子图。
可以看到,通过subplots()
函数和GridSpec()
函数的组合使用,我们可以更加灵活地创建和排布子图。
总结
本文主要介绍了Matplotlib中的subplots()
函数和GridSpec()
函数。使用这两个函数可以帮助我们更方便地进行子图的排布和绘制。subplots()
函数适用于简单的子图排列,而GridSpec()
函数则更加灵活,可以控制每个子图的位置和大小。在实际应用中,我们可以根据自己的需要来选择合适的方法。同时,在使用GridSpec()
函数时,建议先使用subplots()
函数创建一个包含所需子图数量的图形对象,然后再使用get_gridspec()
方法获取相应网格对象。