Matplotlib 小刻度

Bot*_*ond 5 python matplotlib

我正在尝试自定义 matplotlib 图中的小刻度。考虑以下代码:

import pylab as pl
from matplotlib.ticker import AutoMinorLocator

fig, ax = pl.subplots(figsize=(11., 7.4))

x = [1,2,3, 4]
y = [10, 45, 77, 55]
errorb = [20,66,58,11]

pl.xscale("log")

ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

pl.tick_params(which='both', width=1)
pl.tick_params(which='minor', length=4, color='g')
pl.tick_params(axis ='both', which='major', length=8, labelsize =20, color='r' )

pl.errorbar(x, y, yerr=errorb)
#pl.plot(x, y)

pl.show()
Run Code Online (Sandbox Code Playgroud)

据我了解,AutoMinorLocator(n)应该在每个主要刻度之间插入 n 个次要刻度,这是在线性刻度上发生的情况,但根本无法弄清楚在对数刻度上放置次要刻度背后的逻辑。errorbar()最重要的是,使用时比使用简单时有更多的小问题plot()

tmd*_*son 3

AutoMinorLocator 仅设计用于线性刻度:

ticker文档中:

自动次要定位器

当轴是线性且主要刻度均匀分布时,次要刻度的定位器。它将主要刻度间隔细分为指定数量的次要间隔,根据主要间隔默认为 4 或 5。

AutoMinorLocator文档

根据主要刻度的位置动态查找次要刻度位置。假设比例是线性的并且主要刻度线间隔均匀。

您可能想将其用于LogLocator您的目的。

例如,要将主要刻度放在基数 10 上,将次要刻度放在绘图上的 2 和 5 处(或每个base*i*[2,5]),您可以:

ax.xaxis.set_major_locator(LogLocator(base=10))
ax.xaxis.set_minor_locator(LogLocator(base=10,subs=[2.0,5.0]))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述