monostop
monostop

Reputation: 569

Matplotlib tick labels

Is there a way to render the tick labels just right inside the axes, i.e, something like the direction property there is on the ticks themself?

Right now I'm setting the x property to a positive value on the ticklabels to draw them inside of the axis, i.e.,

ax2.set_yticklabels(['0', '2500', '5000', '7500'], minor=False, x=0.05)

But this doesn't really work on resizable plots, as the 0.05 figure is absolute (and too big on big plots).

Any ideas?

Upvotes: 0

Views: 675

Answers (1)

deinonychusaur
deinonychusaur

Reputation: 7304

I'm assuming that ax2 is constructed as ax2 = ax.twinx(), which is to say that it is on the right side of the axes.

You could do something like the following:

ax2.set_yticklabels(['0', '2500', '5000', '7500'], minor=False, horizontalalignment='right')
for tick in ax2.yaxis.get_major_ticks():                                        
        tick.set_pad(-8)

If you want the left side axis on the inside too, then you'd simply switch the horizontal alignment to 'left' and change the pad from -8 to -25.

The two numbers might not be exact and could depend on other matplotlib settings you might have (e.g. length of major ticks) so you may want to increase or decrease those values slightly.

Upvotes: 0

Related Questions