user3017048
user3017048

Reputation: 2911

Place labels both inside and outside of axis

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?

Upvotes: 0

Views: 1411

Answers (2)

B. M.
B. M.

Reputation: 18628

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

Upvotes: 1

djangoliv
djangoliv

Reputation: 1788

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

Upvotes: 1

Related Questions