Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

python - Colormap for errorbars in x-y scatter plot using matplotlib

I have a time series of data for which I have the quantity, y, and its error, yerr. I would now like to create a plot that shows y against phase (i.e. time / period % 1) with vertical errorbars (yerr). For this, I typically use pyplot.errorbar(time, y, yerr=yerr, ...)

However, I would like to use a colorbar/map to indicate the value of time in this same plot.

What I thus do is the following:

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

Unfortunately, this will plot unicolored errorbars (default is blue). Since I have ~1600 points per plot, this makes the colormap of the scatter plot disappear behind the error bars. Here's a picture shows what I mean:

enter image description here

Is there a way that I can get the error bars to be plotted using the same colormap as the one used in the scatter plot? I don't want to call errorbar 1600 times...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In addition to changing the color, another suggestion is to change the zorder of the error bars versus the scatter plot. This focuses the user on the data and draws out the general shape of the errors (which I think is your intention).

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...