Python 如何仅在 y 轴 matplotlib 上打开小刻度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12711202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to turn on minor ticks only on y axis matplotlib
提问by emad
How can I turn the minor ticks only on y axis on a linear vs linear plot?
如何在线性与线性图上仅在 y 轴上转动小刻度?
When I use the function minor_ticks_onto turn minor ticks on, they appear on both x and y axis.
当我使用该函数minor_ticks_on打开小刻度时,它们同时出现在 x 和 y 轴上。
采纳答案by emad
Nevermind, I figured it out.
没关系,我想通了。
ax.tick_params(axis='x', which='minor', bottom=False)
回答by John Vinyard
Here's another way I found in the matplotlib documentation:
这是我在matplotlib 文档中找到的另一种方法:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
a = np.arange(100)
ml = MultipleLocator(5)
plt.plot(a)
plt.axes().yaxis.set_minor_locator(ml)
plt.show()
This will place minor ticks on onlythe y-axis, since minor ticks are off by default.
这将只在 y 轴上放置小刻度,因为默认情况下小刻度是关闭的。
回答by marisano
Also, if you only want minor ticks on the actual y-axis, rather than on both the left and right-hand sides of the graph, you can follow the plt.axes().yaxis.set_minor_locator(ml)with plt.axes().yaxis.set_tick_params(which='minor', right = 'off'), like so:
此外,如果您只想要在实际 y 轴上的小刻度,而不是在图形的左侧和右侧,您可以按照plt.axes().yaxis.set_minor_locator(ml)with plt.axes().yaxis.set_tick_params(which='minor', right = 'off'),如下所示:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
a = np.arange(100)
ml = MultipleLocator(5)
plt.plot(a)
plt.axes().yaxis.set_minor_locator(ml)
plt.axes().yaxis.set_tick_params(which='minor', right = 'off')
plt.show()
回答by Evgeni Sergeev
To set minor ticks at custom locations:
在自定义位置设置小刻度:
ax.set_xticks([0, 10, 20, 30], minor=True)
回答by spinup
To clarify the procedure of @emad's answer, the steps to show minor ticks at default locations are:
为了澄清@emad 的回答过程,在默认位置显示小刻度的步骤是:
- Turn on minor ticks for an axes object, so locations are initialized as Matplotlib sees fit.
- Turn off minor ticks that are not desired.
- 打开轴对象的小刻度,以便在 Matplotlib 认为合适时初始化位置。
- 关闭不需要的小刻度。
A minimal example:
一个最小的例子:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.plot([1,2])
# Currently, there are no minor ticks,
# so trying to make them visible would have no effect
ax.yaxis.get_ticklocs(minor=True) # []
# Initialize minor ticks
ax.minorticks_on()
# Now minor ticks exist and are turned on for both axes
# Turn off x-axis minor ticks
ax.xaxis.set_tick_params(which='minor', bottom=False)
Alternative Method
替代方法
Alternatively, we can get minor ticks at default locations using AutoMinorLocator:
或者,我们可以使用AutoMinorLocator以下命令在默认位置获得次要刻度:
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
fig, ax = plt.subplots()
plt.plot([1,2])
ax.yaxis.set_minor_locator(tck.AutoMinorLocator())
Result
结果
Either way, the resulting plot has minor ticks on the y-axis only.
无论哪种方式,结果图都只在 y 轴上有小刻度。


