Matplotlib 简单教程 7:多字图:matplotlib.gridspec.GridSpec()
1、matplotlib.gridspec.GridSpec() 介绍
👏
matplotlib.gridspec.GridSpec用于创建一个网格布局,可用于在matplotlib中灵活地排列子图。
👏matplotlib.gridspec.GridSpec(nrows, ncols, figure=None, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None)
| 参数解释 |
|---|
1.1、nrows:整数,指定网格的行数。 |
1.2、ncols:整数,指定网格的列数。 |
1.3、figure:可选的matplotlib.figure.Figure对象。如果未提供,则使用当前活动的图形。 |
1.4、left:浮点数,指定子图左侧边缘在图形宽度上的位置。 |
1.5、bottom:浮点数,指定子图底部边缘在图形高度上的位置。 |
1.6、right:浮点数,指定子图右侧边缘在图形宽度上的位置。 |
1.7、top:浮点数,指定子图顶部边缘在图形高度上的位置。 |
1.8、wspace:浮点数,指定子图之间的水平间距,以图形宽度的比例表示。 |
1.9、hspace:浮点数,指定子图之间的垂直间距,以图形高度的比例表示。 |
1.10、width_ratios:可迭代对象,指定各列的宽度比例。 |
1.11、height_ratios:可迭代对象,指定各行的高度比例。 |
1.1、参数: nrows, ncols
👏
nrows参数用于指定网格的行数。ncols参数用于指定网格的列数。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 3)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[0, 2])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 0])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
ax5 = fig.add_subplot(gs[1, 1])
ax5.plot([2, 1, 3], [5, 4, 6])
ax5.set_title('Subplot 5')
ax6 = fig.add_subplot(gs[1, 2])
ax6.plot([3, 2, 1], [6, 5, 4])
ax6.set_title('Subplot 6')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122901.png', dpi=300)
plt.show()

1.3、参数: figure
👏
figure参数用于指定要在其中创建网格的matplotlib.figure.Figure对象。如果未提供,则使用当前活动的图形。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig1 = plt.figure()
gs = gridspec.GridSpec(2,2,figure=fig1)
ax1 = fig1.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot in fig1')
fig2 = plt.figure()
gs2 = gridspec.GridSpec(2, 2, figure=fig2)
ax2 = fig2.add_subplot(gs2[0, 0])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot in fig2')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122902.png', dpi=300)
plt.show()

1.4、参数: left
👏
left参数指定子图左侧边缘在图形宽度上的位置。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2, left=0.2)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[1, 0])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 1])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122904.png', dpi=300)
plt.show()

1.5、参数: bottom
👏
bottom参数指定子图底部边缘在图形高度上的位置。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2, bottom=0.5)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[1, 0])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 1])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122905.png', dpi=300)
plt.show()

1.6、参数: right
👏
right参数指定子图右侧边缘在图形宽度上的位置。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2, right=0.8)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[1, 0])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 1])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122906.png', dpi=300)
plt.show()

1.7、参数: top
👏
top参数指定子图顶部边缘在图形高度上的位置。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2, top=0.6)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[1, 0])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 1])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122907.png', dpi=300)
plt.show()

1.8、参数: wspace
👏
wspace参数指定子图之间的水平间距,以图形宽度的比例表示。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2, wspace=0.6)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[1, 0])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 1])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122908.png', dpi=300)
plt.show()

1.9、参数: hspace
👏
hspace参数指定子图之间的垂直间距,以图形高度的比例表示。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2, hspace=0.6)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[1, 0])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
ax4 = fig.add_subplot(gs[1, 1])
ax4.plot([1, 3, 2], [4, 6, 5])
ax4.set_title('Subplot 4')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122909.png', dpi=300)
plt.show()

1.10、参数: width_ratios
👏
width_ratios参数是一个可迭代对象,用于指定各列的宽度比例。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(1, 3, width_ratios=[1, 2, 3])
fig = plt.figure()
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
ax3 = fig.add_subplot(gs[0, 2])
ax3.plot([3, 1, 2], [6, 4, 5])
ax3.set_title('Subplot 3')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122910.png', dpi=300)
plt.show()

1.11、参数: height_ratios
👏
height_ratios参数是一个可迭代对象,用于指定各行的高度比例。
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 2])
fig = plt.figure()
ax1 = fig.add_subplot(gs[0])
ax1.plot([1, 2, 3], [4, 5, 6])
ax1.set_title('Subplot 1')
ax2 = fig.add_subplot(gs[1])
ax2.plot([2, 3, 1], [5, 6, 4])
ax2.set_title('Subplot 2')
# 自动调整化图形布局
plt.tight_layout()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_2024122911.png', dpi=300)
plt.show()

👏总结:
matplotlib.gridspec.GridSpec用于创建一个网格布局,可用于在matplotlib中灵活地排列子图。
Python 端到端的机器学习AI入门:详细介绍机器学习建模过程,步骤细节;以及人工智能的分阶段学习线路图。 🚀 点击查看 |
SQL + Pandas 练习题SQL 练习题目,使用 Pandas 库实现,使用 Sqlalchemy 库查看 SQL 代码血缘关系。 🚀 点击查看 |
Python 数据可视化介绍了有关 Matplotlib,Seaborn,Plotly 几个 Python 绘图库的简单使用。 🚀 点击查看 |
更多推荐



所有评论(0)