问题描述
由于它们绘制在绘图区域内,因此许多matplotlib绘图中的数据掩盖了轴刻度.更好的方法是绘制从轴向外延伸的刻度线,这是R的绘图系统ggplot
中的默认值.
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.
理论上,这可以通过分别用xc和y轴刻度线的TICKDOWN
和TICKLEFT
线型重绘刻度线来完成:
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()
但是实际上,这只是解决方案的一半,因为get_xticklines
和get_yticklines
方法仅返回主要刻度线.较小的滴答声仍然指向内部.
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?
推荐答案
在您的matplotlib配置文件matplotlibrc中,您可以设置:
In your matplotlib config file, matplotlibrc, you can set:
xtick.direction : out # direction: in or out
ytick.direction : out # direction: in or out
,默认情况下,这将同时将主要和次要的滴答声向外绘制,例如R.对于单个程序,只需执行以下操作:
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'
这篇关于在matplotlib中,如何绘制从轴向外指向的R型轴刻度线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!