【问题标题】:Plotting error bars in matplotlib that match scatter colours在 matplotlib 中绘制与散点颜色匹配的误差线
【发布时间】:2017-08-13 19:16:54
【问题描述】:

我已经看到了很多与这个问题相同的问题,但在他们准确回答我的问题或者我可以应用它们之前,它们似乎总是有点分歧。

我希望以与散点图点相同的配色方案绘制误差线。如果我的值绘制在 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

我对一般的颜色情况感到很困惑,因此目前非常感谢任何帮助。干杯。

【问题讨论】:

    标签: python plot colors scatter errorbar


    【解决方案1】:

    我认为这在 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()
    

    这给了this plot

    显然这对于​​大量的点来说不是很有效!

    【讨论】:

    • 这对我来说是完美的,因为我没有使用大量的积分。遗憾的是,我已经提交了我的项目,只是降低了一些不透明度条,这有点符合我的要求,但我下次会使用它!
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 2021-06-13
    • 2021-12-26
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    相关资源
    最近更新 更多