0

As I have very long string labels and some other shorter labels for one axis, I would like to place the long labels outside and the short labels inside the axis. Is that possible?

A basic example would be this:

fig, ax = plt.subplots()
ax.boxplot(np.random.randn(100), vert=0) 
longlabs = ['foofoofoofoofoofoofoo']
shortlabs= ['bar']
ax.yaxis.set_ticklabels(longlabs)

Where/how to place shortlabs at the inner side of the axis?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user3017048
  • 2,711
  • 3
  • 22
  • 32
  • The line `ax.yaxis.set_yticklabels(longlabs)` does not run. – Julien Oct 26 '15 at 13:41
  • Sorry, that was a typo - one "y" too much. I edited my question. Thank you! – user3017048 Oct 26 '15 at 13:51
  • Embarrassingly, I could not produce a graph of the boxplot to actually look at. It showed an empty plot. Without knowing more, could you instead draw text inside the plot: http://stackoverflow.com/questions/5812169/draw-text-inside-pylab-figure-window ? – Julien Oct 26 '15 at 14:12
  • That's what I kept in mind as a workaround. However, it would be nice if there was no trouble aligning the text to the ticks. – user3017048 Oct 26 '15 at 18:19

2 Answers2

1

You can do something like this:

longlabs = ['foofoofoofoofoofoofoo  bar']
ax.yaxis.set_ticklabels(longlabs,position=(0.06,0))

But I think you should use vertical alignment for long text:

ax.yaxis.set_ticklabels(longlabs, rotation='vertical')

Hope this helps

djangoliv
  • 1,698
  • 14
  • 26
1

ticklabels are not the same as labels. a trick that looks good on this example :

ax.yaxis.set_label_coords(.05,0.5)
ax.yaxis.set_label_text('foooooooooooooooooooooo\n\nbar')
B. M.
  • 18,243
  • 2
  • 35
  • 54