I've seen quite a few questions along the same vein as this one but they always seem to diverge a little before they answer my question exactly or I can apply them.
I am looking to plot error bars in the same colour scheme as my scatter graph points. If my values were plotted on an x and y axis and I wished them to vary colour with another Z value logarithmically, currently I have:
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()
This results in an error of the form:
ValueError: to_rgba: Invalid rgba arg ... length of rgba sequence should be either 3 or 4
I am quite confused with the colour situation in general so any help would be much appreciated at the moment. Cheers.
I think this is surprisingly hard to do in matplotlib. The only way I've found is to use a for loop, and plot each point individually.
For example
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()
which gives this plot
Obviously this won't be very efficient for large numbers of points!