在matplotlib图的中间水平将y标签居中

时间:2020-06-17 00:16:09

标签: python matplotlib

我正在尝试在绘图中将y标签水平居中。我希望标签能像平常一样显示,除了居中而不是沿着图的侧面。

import matplotlib
import matplotlib.pyplot as plt

y = np.arange(0, 11, 1)
x = np.arange(0, 11, 1)
fig, ax = plt.subplots(figsize=(10,50))
ax.xaxis.tick_top() 
offset = matplotlib.transforms.ScaledTranslation(0.5, 0, fig.transFigure)
for label in ax.yaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)
plt.margins(y=0.005)
plt.yticks(np.arange(0, len(y)+1, 1))
plt.plot(y, x, lw=2)
plt.show()

(如果您注意到我交换了x和y数组,那是因为我也在尝试旋转图形)。

我已经得到了一些接近我想要的东西,尽管我不确定这实际上是否居中,而且似乎还稍微降低了标签,这不是我想要的。我不确定为什么会这样,因为我只在偏移量中设置了x值。

如何使标签居中而不向下移动?

0 个答案:

没有答案