我有可变长度的刻度标签,我想将它们对齐到左边(即在较短的标签和 y 轴之间有一个空间)。有什么合理的方法可以做到这一点吗?使用水平对齐“左”将它们向左对齐,但它们都从轴开始,所以它们最终在图中。所以另一个问题可能是——我可以改变他们的起点吗?
演示代码:
import numpy as np
import matplotlib.pyplot as plt
ticks = ["Lorem ipsum dolor sit amet, consectetur adipisicin", "g elit, sed do", "eiusmod tempor incididunt ut labo", "re et dolore magna ali", "qua. Ut en", "im ad minim veniam, quis nostr", "ud exercitation ullamco labo", "ris nisi ut aliquip ex ea c", "ommodo co", "nsequat. Duis aute irure dolor in rep"]
data = [5,1,2,4,1,4,5,2,1,5]
ind = np.arange(len(data))
fig = plt.figure()
ax = plt.subplot(111)
ax.barh(ind, data, 0.999)
ax.set_yticks(ind + 0.5)
r = ax.set_yticklabels(ticks)#, ha = 'left')
fig.set_size_inches(12, 8)
fig.savefig(r'C:\try.png', bbox_extra_artists=r, bbox_inches='tight')