如何在matplotlib中的ticklabels和axis之间添加空格?

时间:2022-04-17 23:44:05

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.

我成功地增加了我的刻度标签的字体,但现在它们太靠近轴了。我想在刻度标签和轴之间添加一点呼吸空间。

3 个解决方案

#1


46  

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

如果您不想全局更改间距(通过编辑rcParams),并希望采用更清晰的方法,请尝试以下操作:

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

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

or for just x axis

或仅用于x轴

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

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

#2


30  

It looks like matplotlib respects these settings as rcParams:

看起来matplotlib将这些设置视为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.)

我查看了源代码,似乎没有任何其他方式以编程方式设置它们。 (tick.set_pad()看起来它试图做正确的事情,但是当构造Ticks时,似乎设置了填充,之后无法更改。)

#3


13  

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

这可以使用set_pad完成,但您必须重置标签...

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

#1


46  

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

如果您不想全局更改间距(通过编辑rcParams),并希望采用更清晰的方法,请尝试以下操作:

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

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

or for just x axis

或仅用于x轴

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

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

#2


30  

It looks like matplotlib respects these settings as rcParams:

看起来matplotlib将这些设置视为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.)

我查看了源代码,似乎没有任何其他方式以编程方式设置它们。 (tick.set_pad()看起来它试图做正确的事情,但是当构造Ticks时,似乎设置了填充,之后无法更改。)

#3


13  

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

这可以使用set_pad完成,但您必须重置标签...

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