How do I add space between the ticklabels and the axes in matplotlib?

Posted on

Question :

How do I add space between the ticklabels and the axes in matplotlib?

I’ve increased the font of my ticklabels successfully, but now they’re too close to the axis. I’d like to add a little breathing room between the ticklabels and the axis.

Answer #1:

If you don’t want to change the spacing globally (by editing your rcParams), and want a cleaner approach, try this:

ax.tick_params(axis='both', which='major', pad=15)

or for just x axis

ax.tick_params(axis='x', which='major', pad=15)

Answered By: wronk

Answer #2:

It looks like matplotlib respects these settings as rcParams:

pylab.rcParams['xtick.major.pad']='8'
pylab.rcParams['ytick.major.pad']='8'

Set those before you create any figures and you should be fine.

I’ve looked at the source code and there doesn’t appear to be any other way to set them programmatically. (tick.set_pad() looks like it tries to do the right thing, but the padding seems to be set when the Ticks are constructed and can’t be changed after that.)

Answered By: abjennings

Answer #3:

This can be done using set_pad but you then have to reset the label…

for tick in ax.get_xaxis().get_major_ticks():
    tick.set_pad(8.)
    tick.label1 = tick._get_text1()
Answered By: tom10

Leave a Reply

Your email address will not be published. Required fields are marked *