如何在线性与线性图上仅转动 y 轴上的小刻度?
当我使用函数
minor_ticks_on
打开小刻度时,它们会出现在 x 轴和 y 轴上。
没关系,我已经明白了。
ax.tick_params(axis='x', which='minor', bottom=False)
这是我在 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()
这将仅在 y 轴上放置小刻度,因为默认情况下小刻度处于关闭状态。
打开坐标区对象的小刻度,以便按照 Matplotlib 认为合适的方式初始化位置。
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)
替代方法
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())
结果
ax.set_xticks([0, 10, 20, 30], minor=True)
plt.axes().yaxis.set_minor_locator(ml)
后面加上
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()
from matplotlib.ticker import MultipleLocator
ax.xaxis.set_minor_locator(MultipleLocator(#))
ax.yaxis.set_minor_locator(MultipleLocator(#))
# refers to the desired interval between minor ticks.