简体   繁体   English

调整单个子图间距

[英]Adjust individual subplot spacing

fig = plt.figure(figsize=(14, 14))
ax0 = fig.add_subplot(12, 2, (1, 9))
ax1 = fig.add_subplot(12, 2, (2, 10))
ax2 = fig.add_subplot(14, 2, (13, 14))
ax3 = fig.add_subplot(14, 2, (15, 16))

for ax in (ax0, ax1, ax2, ax3):
    ax.set_xticks([])
    ax.set_yticks([])
fig.subplots_adjust(hspace=.25, wspace=.02)

yields产量

Can I reduce vertical spacing between bottom two subplots ( ax2, ax3 ) only?我可以只减少底部两个子图 ( ax2, ax3 ) 之间的垂直间距吗?

This is a bit hacky, but you could play with the bounding boxes of each subplot to move that of ax3 closer to ax2 (here touching):这有点 hacky,但您可以使用每个子图的边界框将 ax3 的边界框移动到更接近 ax2 的位置(此处为触摸):

fig = plt.figure(figsize=(5, 5))        # Made smaller for the example
ax0 = fig.add_subplot(12, 2, (1, 9))
ax1 = fig.add_subplot(12, 2, (2, 10))
ax2 = fig.add_subplot(14, 2, (13, 14))
ax3 = fig.add_subplot(14, 2, (15, 16))

for ax in (ax0, ax1, ax2, ax3):
    ax.set_xticks([])
    ax.set_yticks([])

fig.subplots_adjust(hspace=.25, wspace=.02)

# Below is where the m"hack"gic happens
pos_ax3 = ax3.get_position()
pos_ax2 = ax2.get_position()
ywidth = pos_ax3.y1 - pos_ax3.y0

yspace = 0
pos_ax3.y0 = pos_ax2.y0 - ywidth * yspace
pos_ax3.y1 = pos_ax2.y0 - ywidth * (1 + yspace)
ax3.set_position(pos_ax3)

Output:输出:

手动移动轴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM