如何仅在 y 轴上打开小刻度

问题描述 投票:0回答:6

如何在线性与线性图上仅转动 y 轴上的小刻度?

当我使用函数

minor_ticks_on
打开小刻度时,它们会出现在 x 轴和 y 轴上。

python matplotlib axis yticks
6个回答
76
投票

没关系,我已经明白了。

ax.tick_params(axis='x', which='minor', bottom=False)

34
投票

这是我在 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 轴上放置小刻度,因为默认情况下小刻度处于关闭状态。


22
投票

打开坐标区对象的小刻度,以便按照 Matplotlib 认为合适的方式初始化位置。
  1. 关闭不需要的小刻度。
  2. 一个最小的例子:

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())

结果

无论哪种方式,生成的图仅在 y 轴上有较小的刻度。


21
投票

ax.set_xticks([0, 10, 20, 30], minor=True)



5
投票
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()



3
投票

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.

© www.soinside.com 2019 - 2024. All rights reserved.