python - Plotting 'vectors' with different colors in matplotlib -
my data :
import matplotlib.pyplot plt datetime import * import matplotlib my_vectors =[['120819152800063',10,189, 8], ['120819152800063', 10,184, 8], ['120819152800063', 0,190, 43], ['120819152800067', 8,67, 10], ['120819152800067', 8,45, 10], ['120819152800073', 10,31, 8], ['120819152800073', 10,79, 8], ['120819152800073', 7,102, 25], ['120819152800075', 125,0, 13]]
i'm trying plot vectors represent data, cheated(i represent line represent body of vector , 2 points beginning , end), want color body of each 'vector'.
timestring = zip(*my_vectors)[0] timedatetime=[datetime.strptime(atime, '%y%m%d%h%m%s%f') atime in timestring] timedate=matplotlib.dates.date2num(timedatetime) # represent data time x = tuple([int(x) x in timestring]) # represent data sender y = zip(*my_vectors)[1] # represent data type u = zip(*my_vectors)[2] # represent data receiver v = zip(*my_vectors)[3] # 'body' of vectors plt.vlines(timedate,y,v,colors='r') # beginning of vectors plt.plot_date(timedate,y,'b.',xdate=true) # end of vectors plt.plot_date(timedate,v,'y.') plt.show()
to have better read of data, need change color each data type don't know how many data type i'll have. how can this?
i have read question don't understand answer :
setting different color each series in scatter plot on matplotlib
you can access different colors using colormap. module matplotlib.cm provides colormaps or can create own. this demo shows available colormaps , names. colormaps contain 256 different colors. can iterate through data , assign different color each vector done in example linked to. if have more 256 data points you'll need repeat colors or use larger colormap might limits of spectral resolution anyway.
here's example based on question.
from matplotlib import pyplot plt import matplotlib.cm cm import numpy np # fake data timedate = np.arange(256) y = timedate * 1.1 + 2 v = timedate * 3 + 1 # select color map named rainbow cmap = cm.get_cmap(name='rainbow') # plot each vector different color colormap. ind, (t, y, v) in enumerate(zip(timedate, y, v)): plt.vlines(t,y,v ,color = cmap(ind)) plt.show()
alternately, can access rgba values colormap directly:
plt.vlines(timedate,y,v,colors=cmap(np.arange(256)))
Comments
Post a Comment