2015-10-14 66 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