gpt4 book ai didi

matplotlib - 如何增加两个特定 matplotlib 子图之间的水平空间 (hspace)?

转载 作者:行者123 更新时间:2023-12-05 06:02:38 24 4
gpt4 key购买 nike

f = plt.figure(figsize=(12,10))

ax1 = f.add_subplot(411)
ax2 = f.add_subplot(422)
ax3 = f.add_subplot(423)
ax4 = f.add_subplot(424)
ax5 = f.add_subplot(425)
ax6 = f.add_subplot(426)
ax7 = f.add_subplot(427)
ax8 = f.add_subplot(428)

我想增加两行之间的空间:ax1 和 ax2-ax3。其他空间应保持不变。使用“f.subplots_adjust(hspace = 0.2, wspace= 0.25)”调整所有子图的间距。我该怎么做才能仅增加最顶层子图的 hspace?

enter image description here

最佳答案

import matplotlib.pyplot as plt 

fig, axs = plt.subplot_mosaic([['top', 'top'],['left1', 'right1'], ['left2', 'right2']],
constrained_layout=True)
axs['top'].set_xlabel('Xlabel\n\n')
plt.show()

这将使所有 y 轴的大小相同。如果这对您不重要,那么@r-beginners 的回答会很有帮助。请注意,您不需要使用子图马赛克,尽管它是一个有用的新功能。

enter image description here

如果您不担心轴大小匹配,那么比上面建议的方法稍微好一点的方法是使用新的子图功能:

import matplotlib.pyplot as plt 

fig = plt.figure(constrained_layout=True)

subfigs = fig.subfigures(2, 1, height_ratios=[1, 2], hspace=0.15)

# top
axtop = subfigs[0].subplots()

# 2x2 grid
axs = subfigs[1].subplots(2, 2)

plt.show()

enter image description here

关于matplotlib - 如何增加两个特定 matplotlib 子图之间的水平空间 (hspace)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66880397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com