[英]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.我想在刻度标签和轴之间添加一点呼吸空间。
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)
or for just x axis 或仅用于x轴
ax.tick_params(axis='x', which='major', pad=15)
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时似乎设置了填充,之后无法更改。)
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()
axes
plots, but there are other options for a figure
, which can all be positioned by specifying the x=
and y=
parameter.其他答案对于axes
图非常有用,但是对于figure
,还有其他选项,这些选项都可以通过指定x=
和y=
参数来定位。
from matplotlib.cbook import get_sample_data
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(3, 5, figsize=(8, 5), constrained_layout=True,
sharex=True, sharey=True)
fname = get_sample_data('percent_bachelors_degrees_women_usa.csv',
asfileobj=False)
gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True)
majors = ['Health Professions', 'Public Administration', 'Education',
'Psychology', 'Foreign Languages', 'English',
'Art and Performance', 'Biology',
'Agriculture', 'Business',
'Math and Statistics', 'Architecture', 'Physical Sciences',
'Computer Science', 'Engineering']
for nn, ax in enumerate(axs.flat):
ax.set_xlim(1969.5, 2011.1)
column = majors[nn]
column_rec_name = column.replace('\n', '_').replace(' ', '_')
line, = ax.plot('Year', column_rec_name, data=gender_degree_data, lw=2.5)
ax.set_title(column, fontsize='small', loc='left', y=1.05) # move the axes title
ax.set_ylim([0, 100])
ax.tick_params(axis='both', which='major', pad=15) # move the tick labels
ax.grid()
fig.supxlabel('Year', y=-0.15) # with adjusted position
fig.supylabel('Percent Degrees Awarded To Women', x=-0.05) # with adjusted position
fig.suptitle('Majors', y=1.15) # with adjusted position
plt.show()
You can specify labelpad = n
, when labelling your axes, for giving some space between ticklabels and the axis.您可以在标记轴时指定labelpad = n
,以便在刻度标签和轴之间留出一些空间。
from matplotlib import pyplot as plt
plt.xlabel("X-axis Label", labelpad = 10)
plt.ylabel("Y-axis Label", labelpad = 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.