我已经看到了很多与此问题相同的问题,但在他们完全回答我的问题或者我可以应用它们之前,它们似乎总是有点分歧。
我希望用与散点图相同的颜色方案绘制误差线。如果我的值是在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
我对一般的颜色情况很困惑,所以目前任何帮助都会非常感激。欢呼声。
答案 0 :(得分:0)
我认为在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
显然,这对于大量积分来说效率不高!