2015-10-14 30 views
3

我正在试图定制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() 

据我了解,AutoMinorLocator(n)应该是每个主刻度之间插入N次刻度,这是线性比例会发生什么,但根本无法找出逻辑布局的背后在一个logscale上的小勾号。最重要的是,当使用errorbar(),然后使用简单的plot()时,会有更多次要的滴答声。

回答

2

AutoMinorLocator仅设计为线性尺度工作:

ticker documentation

AutoMinorLocator

定位符次要蜱当轴是线性和主刻度均匀间隔。它将主要滴答间隔细分为指定数量的小间隔,默认为4或5,具体取决于主要间隔。

而且AutoMinorLocator documentation

动态查找基于主要刻度线的位置次要刻度位置。 假设比例尺是线性的和主要的蜱均匀间隔。

您可能想要使用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)) 

enter image description here

+0

工作就像一个魅力!感谢您的彻底解答! – Botond