在 matplotlib 中,如何绘制从轴向外指向的 R 样式轴刻度?

发布于 2024-11-14 14:04:12 字数 1345 浏览 2 评论 0原文

由于轴刻度是在绘图区域内绘制的,因此许多 matplotlib 图中的数据会遮挡轴刻度。更好的方法是绘制从轴向外延伸的刻度,这是 R 绘图系统 ggplot 中的默认设置。

理论上,这可以通过分别为 x 轴和 y 轴刻度线使用 TICKDOWNTICKLEFT 线条样式重新绘制刻度线来完成:

import matplotlib.pyplot as plt
import matplotlib.ticker as mplticker
import matplotlib.lines as mpllines

# Create everything, plot some data stored in `x` and `y`
fig = plt.figure()
ax = fig.gca()
plt.plot(x, y)

# Set position and labels of major and minor ticks on the y-axis
# Ignore the details: the point is that there are both major and minor ticks
ax.yaxis.set_major_locator(mplticker.MultipleLocator(1.0))
ax.yaxis.set_minor_locator(mplticker.MultipleLocator(0.5))

ax.xaxis.set_major_locator(mplticker.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mplticker.MultipleLocator(0.5))

# Try to set the tick markers to extend outward from the axes, R-style
for line in ax.get_xticklines():
    line.set_marker(mpllines.TICKDOWN)

for line in ax.get_yticklines():
    line.set_marker(mpllines.TICKLEFT)

# In real life, we would now move the tick labels farther from the axes so our
# outward-facing ticks don't cover them up

plt.show()

但实际上,这只是解决方案的一半,因为 get_xticklinesget_yticklines 方法仅返回主要刻度线。小刻度仍然指向内。

轻微蜱虫的解决方法是什么?

Because they are drawn inside the plot area, axis ticks are obscured by the data in many matplotlib plots. A better approach is to draw the ticks extending from the axes outward, as is the default in ggplot, R's plotting system.

In theory, this can be done by redrawing the tick lines with the TICKDOWN and TICKLEFT line-styles for the x-axis and y-axis ticks respectively:

import matplotlib.pyplot as plt
import matplotlib.ticker as mplticker
import matplotlib.lines as mpllines

# Create everything, plot some data stored in `x` and `y`
fig = plt.figure()
ax = fig.gca()
plt.plot(x, y)

# Set position and labels of major and minor ticks on the y-axis
# Ignore the details: the point is that there are both major and minor ticks
ax.yaxis.set_major_locator(mplticker.MultipleLocator(1.0))
ax.yaxis.set_minor_locator(mplticker.MultipleLocator(0.5))

ax.xaxis.set_major_locator(mplticker.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mplticker.MultipleLocator(0.5))

# Try to set the tick markers to extend outward from the axes, R-style
for line in ax.get_xticklines():
    line.set_marker(mpllines.TICKDOWN)

for line in ax.get_yticklines():
    line.set_marker(mpllines.TICKLEFT)

# In real life, we would now move the tick labels farther from the axes so our
# outward-facing ticks don't cover them up

plt.show()

But in practice, that's only half the solution because the get_xticklines and get_yticklines methods return only the major tick lines. The minor ticks remain pointing inward.

What's the work-around for the minor ticks?

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

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

发布评论

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

评论(2

苹果你个爱泡泡 2024-11-21 14:04:12

在 matplotlib 配置文件 matplotlibrc 中,您可以设置:

xtick.direction      : out     # direction: in or out
ytick.direction      : out     # direction: in or out

默认情况下,这将向外绘制主要刻度线,就像 R 一样。对于单个程序,只需执行以下操作:

>> from matplotlib import rcParams
>> rcParams['xtick.direction'] = 'out'
>> rcParams['ytick.direction'] = 'out'

In your matplotlib config file, matplotlibrc, you can set:

xtick.direction      : out     # direction: in or out
ytick.direction      : out     # direction: in or out

and this will draw both the major and minor ticks outward by default, like R. For a single program, simply do:

>> from matplotlib import rcParams
>> rcParams['xtick.direction'] = 'out'
>> rcParams['ytick.direction'] = 'out'
世界等同你 2024-11-21 14:04:12

您至少可以通过两种方式获取次要刻度线:

>>> ax.xaxis.get_ticklines() # the majors
<a list of 20 Line2D ticklines objects>
>>> ax.xaxis.get_ticklines(minor=True) # the minors
<a list of 38 Line2D ticklines objects>
>>> ax.xaxis.get_minorticklines()
<a list of 38 Line2D ticklines objects>

请注意,38 是因为 MultipleLocator 调用也在“主要”位置绘制了次要刻度线。

You can get the minors in at least two ways:

>>> ax.xaxis.get_ticklines() # the majors
<a list of 20 Line2D ticklines objects>
>>> ax.xaxis.get_ticklines(minor=True) # the minors
<a list of 38 Line2D ticklines objects>
>>> ax.xaxis.get_minorticklines()
<a list of 38 Line2D ticklines objects>

Note that the 38 is because minor tick lines have also been drawn at the "major" locations by the MultipleLocator call.

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