2012-04-18 63 views
13

我有一個時間序列的數據,我有數量y和它的錯誤yerr。現在我想創建一個顯示y與垂直錯誤條(yerr)相對的相位(即時間/週期%1)的圖。爲此,我通常使用pyplot.errorbar(time,y,yerr = yerr,...)x-y散點圖中使用matplotlib的錯誤條的顏色映射

但是,我想使用colorbar/map來指示同一圖中的時間值。

什麼因而我做的是以下幾點:

pylab.errorbar(phase, y, yerr=err, fmt=None, marker=None, mew=0) 
pylab.scatter(phase, y, c=time, cmap=cm) 

不幸的是,這將繪製unicolored errorbars(默認爲藍色)。由於每個小區有大約1600個點,這使得散點圖的色彩圖消失在誤差欄後面。這裏有一個圖片顯示我的意思:

enter image description here

有沒有一種方法,我可以得到誤差線使用相同的顏色表在散點圖中使用的繪製?我不想叫errorbar 1600次......

回答

0

您可以使用ecolor可選參數在pylab.errorbar如您使用pylab.scattercolor說法:

pylab.errorbar(phase, y, yerr=err, fmt=None, marker=None, mew=0, ecolor=time) 
+0

@大呼過癮,我想隨機希望他/她errorbars具有相同顏色作爲他/她的數據點。您的解決方案很好,因爲它將誤差線放置在數據點之下並提高圖形的清晰度,但它不能解決誤差線顏色問題。 – 2012-04-19 08:08:55

+0

@ Moi Jaiunvelo:不幸的是,我嘗試過這種方法,但它不適合我。你能解釋一下如何獲取時間數組(類型爲float並且通常從[0,2000.]運行)到一個有效的ecolor數組? – Random 2012-04-20 08:10:50

14

除了改變顏色,另一建議改變誤差線對散點圖的zorder。這將用戶的注意力集中在數據上,並繪出錯誤的一般形狀(我認爲這是您的意圖)。

from pylab import * 

# Generate some random data that looks like yours 
N = 1000 
X = random(N) 
Y = sin(X*5) + X*random(N)*.8 
Z = random(N) 
ERR = X*random(N) 

# These are the new arguments that I used 
scatter_kwargs = {"zorder":100} 
error_kwargs = {"lw":.5, "zorder":0} 

scatter(X,Y,c=Z,**scatter_kwargs) 
errorbar(X,Y,yerr=ERR,fmt=None, marker=None, mew=0,**error_kwargs) 
xlim(0,1) 
show() 

enter image description here

+0

謝謝,掛鉤!我不知道zorder kwarg。雖然這不是我所希望的答案,但它現在會做。 MoiJaiunvelo是對的,理想的情況是我想把誤差線繪製成與數據點相同的顏色。 – Random 2012-04-20 08:12:39

7

我一直在尋找了一段時間的解決方案,我終於找到了一種方法,通過:

from pylab import * 

#data 
time = arange(100.) 
signal = time**2 
error = ones(len(time))*1000 

figure(1) 
#create a scatter plot 
sc = scatter(time,signal,s=20,c=time) 

#create colorbar according to the scatter plot 
clb = colorbar(sc) 

#create errorbar plot and return the outputs to a,b,c 
a,b,c = errorbar(time,signal,yerr=error,marker='',ls='',zorder=0) 

#convert time to a color tuple using the colormap used for scatter 
time_color = clb.to_rgba(time) 

#adjust the color of c[0], which is a LineCollection, to the colormap 
c[0].set_color(time_color) 

fig = gcf() 
fig.show() 
xlabel('time') 
ylabel('signal') 
+0

偉大的解決方案,但帽子的顏色如何?有沒有辦法使用顏色表更新大寫字母的顏色? – aim 2014-07-15 22:49:26

相關問題