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


Question

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.

1
54
6/3/2010 9:19:24 PM

Accepted Answer

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

31
1/12/2015 5:42:45 PM

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)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon