Colormap for errorbars in x-y scatter plot using matplotlib

前端 未结 4 1290
情书的邮戳
情书的邮戳 2020-12-06 05:28

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) wit

4条回答
  •  天涯浪人
    2020-12-06 06:02

    I was looking for the solution for a while and I finally found a way through:

    from pylab import *
    
    #data
    time = arange(100.)
    signal = time**2
    error = ones(len(time))*1000
    
    figure(1)
    #create a scatter plot
    sc = scatter(time,signal,s=20,c=time)
    
    #create colorbar according to the scatter plot
    clb = colorbar(sc)
    
    #create errorbar plot and return the outputs to a,b,c
    a,b,c = errorbar(time,signal,yerr=error,marker='',ls='',zorder=0)
    
    #convert time to a color tuple using the colormap used for scatter
    time_color = clb.to_rgba(time)
    
    #adjust the color of c[0], which is a LineCollection, to the colormap
    c[0].set_color(time_color)
    
    fig = gcf()
    fig.show()
    xlabel('time')
    ylabel('signal')
    

提交回复
热议问题