2011-09-29 41 views
31

如何更改Matplotlib中錯誤欄上限的線寬?如何在matplotlib中設置錯誤欄上限的線寬?

我嘗試下面的代碼:

(_, caplines, _) = matplotlib.pyplot.errorbar(
    data['distance'], data['energy'], yerr=data['energy sigma'], 
    capsize=10, elinewidth=3) 

for capline in caplines: 
    capline.set_linewidth(10) 
    capline.set_color('red') 

pp.draw() 

不幸的是,這將更新瓶蓋的顏色,但並沒有更新線寬的上限

所得效果是類似「脂肪誤差條線/薄帽」如下圖中: enter image description here

這將是不錯的「胖」酒吧蓋,在的情況下; Matplotlib中如何做到這一點? 「手動」繪製條形帽,與plot()一個接一個會工作,但更簡單的替代方案將是最好的。

回答

9

這是基於@華的回答,卻多了幾分簡潔(如果你只是想沒有什麼特別的造型簡單出錯大寫):

distance = [1,3,7,9] 
energy = [10,20,30,40] 
sigma = [1,3,2,5] 

plt.errorbar(distance, 
    energy, 
    sigma, 
    capsize=5, 
    elinewidth=2, 
    markeredgewidth=2) 

enter image description here

29

EOL,你很近..,

distance = [1,3,7,9] 
energy = [10,20,30,40] 
sigma = [1,3,2,5] 

(_, caps, _) = plt.errorbar(distance, energy, sigma, capsize=20, elinewidth=3) 

for cap in caps: 
    cap.set_color('red') 
    cap.set_markeredgewidth(10) 

plt.show 

enter image description here

set_markeredgewidth設置蓋線條的寬度。

Matplotlib對象具有許多屬性,通常很難記住給定對象的正確屬性。 IPython是反省matplotlib的一個非常有用的工具。我用它來分析對應於錯誤頂點線的2Dlines的屬性,並且我發現了其他標記屬性。

乾杯

+0

非常感謝!我們使用了相同的方法,但我不知何故錯過了'set_markeredgewidth'。 :)所以,帽子實際上是Matplotlib的標記,而不是2D線。在我看來,'errorbar()'的'capsize'參數等同於'cap.set_markersize()',所以後者可以被移除,不是嗎? – EOL

+0

當你在評論時,我也意識到這一點。我也改變了圖片。 – joaquin

+0

@joaquin,你如何使用iPython進行「introspecting matplotlib」。這聽起來像是一個非常有用的技巧。 – Blink