【发布时间】:2022-09-28 02:56:19
【问题描述】:
我有一个子图网格,我想调整其中两个之间的空白空间,以便共享的 x 标签居中而不重叠任何一个图。
This question 有一个解决方案,当这些是仅有的两个子图时。但是,我正在努力将其调整为许多网格中的两个特定子图。
这段代码可以用来说明我的问题。
In [1]
fig = plt.figure(figsize = (15, 10))
gs = fig.add_gridspec(2,4)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1:3])
ax3 = fig.add_subplot(gs[0, 3])
ax4 = fig.add_subplot(gs[1, 0])
ax5 = fig.add_subplot(gs[1, 1])
ax6 = fig.add_subplot(gs[1, 2])
ax7 = fig.add_subplot(gs[1, 3])
np.random.seed(19680801)
# Example data
people = (\'Really Really Long Name\', \'Another Long Name\', \'Short Name\', \'Name\', \'N\')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
ax5.barh(y_pos, performance, align=\'center\')
ax5.set_yticks(y_pos, labels=people)
ax5.invert_xaxis()
ax5.set_xlabel(\'Label\')
ax5.set_title(\'Bar 1\')
ax6.barh(y_pos, performance, align=\'center\')
ax6.set_yticks(y_pos, labels=people)
ax6.set_xlabel(\'Label\')
ax6.set_title(\'Bar 2\')
Out [1]
如果我将解决方案应用于此处的链接问题,那么每个子图的空白都会受到影响。我知道这是因为它调用了fig.dpi_scale_trans,这会影响整个数字,但我是变换的新手,不知道用什么代替它
In [2]
fig.tight_layout()
fig.subplots_adjust(wspace=0.7)
plt.setp(axes[0].yaxis.get_majorticklabels(), ha=\'center\')
# Create offset transform by some points in x direction
dx = 60 / 72.
dy = 0 / 72.
offset = mlb.transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)
# apply offset transform to all y ticklabels.
for label in ax6.yaxis.get_majorticklabels():
label.set_transform(label.get_transform() + offset)
Out [2]
标签: matplotlib grid whitespace centering axis-labels