matplotlib: varying color of line to capture natural time parameterization in data -
i trying vary color of line plotted data in 2 arrays (eg. ax.plot(x,y)
). color should vary index x
, y
increases. trying capture natural 'time' parameterization of data in arrays x
, y
.
in perfect world, want like:
fig = pyplot.figure() ax = fig.add_subplot(111) x = myxdata y = myydata # length of x , y 100 ax.plot(x,y,color=[i/100,0,0]) # index x (and y)
to produce line color varying black dark red , on bright red.
i have seen examples work plotting function explicitly parameterized 'time' array, can't work raw data...
the second example 1 want... i've edited fit example, more importantly read comments understand going on:
import numpy np matplotlib import pyplot plt matplotlib.collections import linecollection x = myxdata y = myydata t = np.linspace(0,1,x.shape[0]) # "time" variable # set list of (x,y) points points = np.array([x,y]).transpose().reshape(-1,1,2) print points.shape # out: (len(x),1,2) # set list of segments segs = np.concatenate([points[:-1],points[1:]],axis=1) print segs.shape # out: ( len(x)-1, 2, 2 ) # see we've done here -- we've mapped our (x,y) # points array of segment start/end coordinates. # segs[i,0,:] == segs[i-1,1,:] # make collection of segments lc = linecollection(segs, cmap=plt.get_cmap('jet')) lc.set_array(t) # color segments our parameter # plot collection plt.gca().add_collection(lc) # add collection plot plt.xlim(x.min(), x.max()) # line collections don't auto-scale plot plt.ylim(y.min(), y.max())
Comments
Post a Comment