使用一个 Locator()
可以控制 matplotlib 轴上的次要或主要刻度数。
例如 :
plt.gca().yaxis.set_minor_locator(plt.MultipleLocator(1))
但是上面的行取决于您正在绘制的数据。
我想知道是否有办法从 matplotlib 样式文件(例如
matplotlibrc
)中更改次要刻度数。 ( https://matplotlib.org/users/customizing.html )。例如,您可以通过以下方式管理小刻度样式:xtick.minor.visible : True
xtick.minor.width : 2
xtick.minor.size : 5
但我不知道如何处理次要滴答声。
最佳答案
刻度数实际上并不被视为“样式”属性。因此,您目前无法选择为此使用 rc 文件。
至于默认设置的来源,这是由 AutoMinorLocator
决定的。 ,如果没有给出其他自定义定位器并且打开了次要刻度,则使用它。
matplotlib.ticker.AutoMinorLocator(n=None)
Dynamically find minor tick positions based on the positions of major ticks. The scale must be linear with major ticks evenly spaced.
n
is the number of subdivisions of the interval between major ticks; e.g., n=2 will place a single minor tick midway between major ticks.If
n
is omitted or None, it will be set to 5 or 4.
这为以下解决方法留下了空间。你可以给
AutoMinorLocator
打补丁使用与平常不同的默认值。例如。有 23 个小垃圾箱,# use these lines on top of your matplotlib script
import matplotlib.ticker
class MyLocator(matplotlib.ticker.AutoMinorLocator):
def __init__(self, n=23):
super().__init__(n=n)
matplotlib.ticker.AutoMinorLocator = MyLocator
# Now use matplotlib as usual.
import matplotlib.pyplot as plt
plt.rcParams["xtick.minor.visible"] = True
plt.plot([1,2])
plt.show()
关于python - 如何在 matplotlib 配置文件中定义次要刻度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53634709/