簡體   English   中英

在matplotlib中繪制與散布顏色匹配的誤差線

[英]Plotting error bars in matplotlib that match scatter colours

我已經看到了很多與此問題相同的問題,但在他們完全回答我的問題或者我可以應用它們之前,它們似乎總是有點分歧。

我希望在與散點圖點相同的顏色方案中繪制誤差線。 如果我的值是在x和y軸上繪制的,我希望它們以對數方式改變另一個Z值的顏色,目前我有:

c = np.abs(zVals)
cmhot = plt.get_cmap("plasma")

sc.set_clim(vmin=min(zVals), vmax=max(zVals))

sc = plt.scatter(xVals, yVals, c=c, norm=mplc.LogNorm(), 
s=50, cmap=cmhot, edgecolors='none')

###This section all works fine, it is when I introduce the error bars I struggle

norm = mplc.LogNorm(vmin=min(zVals), vmax=max(zVals)

plt.errorbar(xVals, yVals, yerr = [negyVals,posyVals], c=cmhot(norm(zVals)))
plt.colorbar(sc)


plt.ylim([-10,110])
plt.xlim([1,100])

plt.xscale('log')


plt.show()

這會導致以下形式的錯誤:

ValueError: to_rgba: Invalid rgba arg ... length of rgba sequence should be either 3 or 4

我對一般的顏色情況很困惑,所以任何幫助都會在此刻受到贊賞。 干杯。

我認為在matplotlib中這很難做到。 我發現的唯一方法是使用for循環,並單獨繪制每個點。

例如

plt.figure()

#data
x=np.linspace(-10, 10, 100)
y=np.cos(x)
y_error=0.2+0.5*np.random.randn(len(x))
z=np.linspace(0, 10, 100)

cm=plt.get_cmap('plasma')
plt.scatter(x, y, c=z_values, cmap=cm, zorder=10)


for i, (xval, yval, y_error_val, zval) in enumerate(zip(x, y, y_error, z)):

    #Get the colour from the colourmap
    colour=cm(1.0*zval/np.max(z))
    #(could also just do colour=cm(1.0*i/len(x)) here)
    #(Or Norm(zval) in your case)

    plt.errorbar(xval, yval, yerr=y_error_val, linestyle='', c=colour)

plt.show()

給出了這個情節

顯然,這對於大量積分來說效率不高!

暫無
暫無

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

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