如何在刻度标签和轴之间添加空间

发布于 2024-09-04 03:29:43 字数 53 浏览 5 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白云不回头 2024-09-11 03:29:43

如果您不想全局更改间距(通过编辑 rcParams),并且想要更简洁的方法,请尝试以下操作:

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

或仅用于 x 轴

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

或 y 轴

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

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)

or the y axis

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

绮筵 2024-09-11 03:29:43

看起来 matplotlib 将这些设置视为 rcParams:

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

在创建任何图形之前设置这些设置,应该没问题。

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

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

一袭白衣梦中忆 2024-09-11 03:29:43

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

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

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()
清风不识月 2024-09-11 03:29:43
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()

在此处输入图像描述

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

enter image description here

小清晰的声音 2024-09-11 03:29:43

在标记轴时,您可以指定 labelpad = n,以便在刻度标签和轴之间留出一些空间。

from matplotlib import pyplot as plt

plt.xlabel("X-axis Label", labelpad = 10)
plt.ylabel("Y-axis Label", labelpad = 10)

You can specify labelpad = n, when labelling your axes, for giving some space between ticklabels and the axis.

from matplotlib import pyplot as plt

plt.xlabel("X-axis Label", labelpad = 10)
plt.ylabel("Y-axis Label", labelpad = 10)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文