这是一种可行但不是很方便的方法。您可以在设置xticklabels 时提供position 关键字。这允许您在轴坐标中使用负偏移。如果您手动设置轴的位置和它们之间的间距,您可以计算出这个负偏移量需要多少才能使标签准确地位于两个轴之间的中心。
鉴于您的示例数据:
fig = plt.figure(figsize=(10, 2), facecolor='w')
fig.subplots_adjust(wspace=0.2)
ax1 = fig.add_axes([0.0, 0, 0.4, 1])
ax2 = fig.add_axes([0.6, 0, 0.4, 1])
ax1.barh(y, diff, align='center')
ax1.set_yticks(y)
ax1.yaxis.set_major_formatter(matplotlib.ticker.NullFormatter())
ax2.barh(y, data, align='center')
ax2.set_yticks(y)
ax2.set_yticklabels(['reference', 'something', 'something else', 'nothing', ],
ha='center', position=(-0.25, 0))
坐标轴在图形坐标中的宽度均为 0.4,它们之间的间距为 0.2。这意味着标签必须在图形坐标中为 0.5。由于第二个轴从 0.6 开始,因此图形坐标中的偏移量需要 -0.1。不幸的是,位置应该在轴坐标中给出。坐标区的宽度为 0.4,因此图形坐标中四分之一的坐标区宽度为 0.1。这意味着指定负四分之一的偏移量 -0.25 会将标签放置在两个轴之间。我希望这是有道理的.....
请注意,我将yticklabels 与ha='center' 居中。并且还使您的条居中,因此您在设置ticks 时不必再指定偏移量。
编辑:
您可以通过读取两个轴的位置来自动完成。
def center_ylabels(ax1, ax2):
pos2 = ax2.get_position()
right = pos2.bounds[0]
pos1 = ax1.get_position()
left = pos1.bounds[0] + pos1.bounds[2]
offset = ((right - left) / pos2.bounds[2]) * -0.5
for yt in ax2.get_yticklabels():
yt.set_position((offset, yt.get_position()[1]))
yt.set_ha('center')
plt.setp(ax2.yaxis.get_major_ticks(), pad=0)
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,2))
fig.subplots_adjust(wspace=0.5)
ax1.barh(y, diff, align='center')
ax1.set_yticks(y)
ax1.yaxis.set_major_formatter(matplotlib.ticker.NullFormatter())
ax2.barh(y, data, align='center')
ax2.set_yticks(y)
ax2.set_yticklabels(['reference', 'something', 'something else', 'nothing'])
center_ylabels(ax1, ax2)