我试图在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()
据我所知,AutoMinorLocator(n)应该在每个主要刻度之间插入n个小刻度,这是在线性刻度上发生的,但根本无法弄清楚在日志刻度上放置次刻度的逻辑.最重要的是,当使用errorbar()然后使用简单的plot()时,有更多的小刻度.
最佳答案 AutoMinorLocator仅适用于线性比例:
AutoMinorLocator
locator for minor ticks when the axis is linear and the major ticks are uniformly spaced. It subdivides the major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the major interval.
而AutoMinorLocator
documentation:
Dynamically find minor tick positions based on the positions of major ticks. Assumes the scale is linear and major ticks are evenly spaced.
您可能希望将LogLocator
用于您的目的.
例如,要将主刻度线放在基数10中,并将小标记放在第2点和第5点(或每个基数* 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))