Matplotlib - 多面板图的单个轴标签?

3

我想创建一个多面板图:

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.scatter(1,1)
ax2 = fig.add_subplot(2,1,2,sharex=ax1)
ax2.scatter(1,1)

然后创建一个单独的轴对象,专门用于标签...
dummy = fig.add_subplot(1,1,1)
dummy.set_visible(False)
dummy.yaxis.set_label_text('y label')
dummy.yaxis.label.set_visible(True)

但是它不起作用。我想知道为什么?

1个回答

5

昨天我遇到了类似的问题,我只需使用text()函数就能得到结果。以您的示例为例,代码如下:

import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.scatter(1,1)
ax2 = fig.add_subplot(2,1,2,sharex=ax1)
ax2.scatter(1,1)

ax = fig.add_axes( [0., 0., 1, 1] )
ax.set_axis_off()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.text( 
    .05, 0.5, "Y Label", rotation='vertical',
    horizontalalignment='center', verticalalignment='center'
)

您可能还想使用plt.subplots_adjust()来为标签腾出空间。


不错 - 这个能用。谢谢。你知道为什么我的例子不起作用吗? - hatmatrix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接