簡體   English   中英

Matplotlib錯誤欄使用for循環(用於不同的顏色)

[英]Matplotlib error bar using a for loop (for different colors)

我想繪制一組不同顏色的誤差線。 我的數據點也使用不同的顏色。

目前,我正在使用:

colours = ['r','b','g','k','m']
labels = ['200Mpc','300Mpc','340Mpc','400Mpc','450Mpc']

fig2 = plt.figure(figsize=(7,5))
ax3 = fig2.add_subplot(111)
for a,b,c,d,e,f in zip(r0_array,gamma_array,r0_error,gamma_error,colours,labels):
    ax3.scatter(r0_array,gamma_array,c=e,label=f)
    ax3.errorbar(r0_array,gamma_array,xerr=c,yerr=d,fmt='o',color=e)
ax3.set_xlabel('$r_{0}$',fontsize=14)
ax3.set_ylabel(r'$\gamma$',fontsize=14)
ax3.legend(loc='best')
fig2.show()

這將導致圖形的誤差線和顏色被過度繪制。

在此處輸入圖片說明

我可以看到for循環再次運行5次,因為我可以看到所有的顏色,但是我不知道為什么會這樣!

我發現我犯了一個非常愚蠢的錯誤!

for循環之后,每個值,即a,b,c,d,e,f均取入數組r0_array,gamma_array等內的值。

而不是scatter調用a,b,c,d r0_array, gamma_array,etc..每次都調用整個數組r0_array, gamma_array,etc..

for a,b,c,d,e,f in zip(r0_array,gamma_array,r0_error,gamma_error,colours,labels):
        ax3.scatter(a,b,color=e,label=f)
        ax3.errorbar(a,b,xerr=c,yerr=d,fmt='o')

解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM