在 matplotlib 中,如何绘制从轴向外指向的 R 样式轴刻度?
由于轴刻度是在绘图区域内绘制的,因此许多 matplotlib 图中的数据会遮挡轴刻度。更好的方法是绘制从轴向外延伸的刻度,这是 R 绘图系统 ggplot 中的默认设置。
理论上,这可以通过分别为 x 轴和 y 轴刻度线使用 TICKDOWN
和 TICKLEFT
线条样式重新绘制刻度线来完成:
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_xticklines
和 get_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 matplotlib 配置文件 matplotlibrc 中,您可以设置:
默认情况下,这将向外绘制主要刻度线,就像 R 一样。对于单个程序,只需执行以下操作:
In your matplotlib config file, matplotlibrc, you can set:
and this will draw both the major and minor ticks outward by default, like R. For a single program, simply do:
您至少可以通过两种方式获取次要刻度线:
请注意,38 是因为 MultipleLocator 调用也在“主要”位置绘制了次要刻度线。
You can get the minors in at least two ways:
Note that the 38 is because minor tick lines have also been drawn at the "major" locations by the MultipleLocator call.