13

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?

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')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Korem
  • 11,383
  • 7
  • 55
  • 72

2 Answers2

13

Tested in python 3.11.2, matplotlib 3.7.1

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

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

matplotlib.axis.Axis.set_tick_params

To do your figuring of what the pad should be:

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

yax = ax.get_yaxis()
# find the maximum width of the label on the major ticks
pad = max(T.label1.get_window_extent().width for T in yax.majorTicks)

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

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
tacaswell
  • 84,579
  • 22
  • 210
  • 199
1

There is a simple way to achieve your goal by using mpl_toolkits.

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist


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,axes_class=axisartist.Axes)  # Needs to specify axes_class
ax.barh(ind, data, 0.999)
ax.set_yticks(ind + 0.5)
r = ax.set_yticklabels(ticks)


ax.axis["left"].major_ticklabels.set_ha("left") # Then it's simple to use set_ha.

plt.show()

enter image description here

Z-Y.L
  • 1,740
  • 1
  • 11
  • 15