Matplotlib:将y刻度对齐到左边。

时间:2021-03-10 23:46:05

I have tick labels of variable length, and I want to align them to the left (i.e. to have a space between the shorter ones and the y axis). Is there any reasonable way to do this? Using horizontal alignment 'left' aligns them to the left, but they all start at the axis, so they end up inside the plot. So an alternative question could be - can I change their starting point?

我有可变长度的标记,我想把它们对齐到左边(即在短的和y轴之间有一个空间)。有什么合理的方法吗?使用水平对齐方式向左对齐,但它们都是从轴开始的,所以它们最终在图中。所以另一个问题可能是——我能改变他们的出发点吗?

Code for demonstration:

代码演示:

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')

1 个解决方案

#1


8  

You will just need to add a pad. See matplotlib ticks position relative to axis

您只需要添加一个pad。看到matplotlib位置相对于轴。

yax = ax.get_yaxis()
yax.set_tick_params(pad=pad)

(doc)

(医生)

To do your figuring of what the pad should be:

要想知道pad应该是什么:

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(tight_layout=True) # need tight_layout to make everything fit
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, forward=True) 
# re-size first, the shift needs to be in display units
plt.draw()  # this is needed because get_window_extent needs a renderer to work
yax = ax.get_yaxis()
# find the maximum width of the label on the major ticks
pad = max(T.label.get_window_extent().width for T in yax.majorTicks)

yax.set_tick_params(pad=pad)
plt.draw()

Matplotlib:将y刻度对齐到左边。

#1


8  

You will just need to add a pad. See matplotlib ticks position relative to axis

您只需要添加一个pad。看到matplotlib位置相对于轴。

yax = ax.get_yaxis()
yax.set_tick_params(pad=pad)

(doc)

(医生)

To do your figuring of what the pad should be:

要想知道pad应该是什么:

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(tight_layout=True) # need tight_layout to make everything fit
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, forward=True) 
# re-size first, the shift needs to be in display units
plt.draw()  # this is needed because get_window_extent needs a renderer to work
yax = ax.get_yaxis()
# find the maximum width of the label on the major ticks
pad = max(T.label.get_window_extent().width for T in yax.majorTicks)

yax.set_tick_params(pad=pad)
plt.draw()

Matplotlib:将y刻度对齐到左边。