matplotlib - python: how to plot one line in different colors -
i have 2 list below:
latt=[42.0,41.978567980875397,41.96622693388357,41.963791391892457,...,41.972407378075879] lont=[-66.706920989908909,-66.703116557977069,-66.707351643324543,...-66.718218142021925] now want plot line, separate each 10 of 'latt' , 'lont' records period , give unique color. should do?
there several different ways this. "best" approach depend on how many line segments want plot.
if you're going plotting handful (e.g. 10) line segments, like:
import numpy np import matplotlib.pyplot plt def uniqueish_color(): """there're better ways generate unique colors, isn't awful.""" return plt.cm.gist_ncar(np.random.random()) xy = (np.random.random((10, 2)) - 0.5).cumsum(axis=0) fig, ax = plt.subplots() start, stop in zip(xy[:-1], xy[1:]): x, y = zip(start, stop) ax.plot(x, y, color=uniqueish_color()) plt.show() 
if you're plotting million line segments, though, terribly slow draw. in case, use linecollection. e.g.
import numpy np import matplotlib.pyplot plt matplotlib.collections import linecollection xy = (np.random.random((1000, 2)) - 0.5).cumsum(axis=0) # reshape things have sequence of: # [[(x0,y0),(x1,y1)],[(x0,y0),(x1,y1)],...] xy = xy.reshape(-1, 1, 2) segments = np.hstack([xy[:-1], xy[1:]]) fig, ax = plt.subplots() coll = linecollection(segments, cmap=plt.cm.gist_ncar) coll.set_array(np.random.random(xy.shape[0])) ax.add_collection(coll) ax.autoscale_view() plt.show() 
for both of these cases, we're drawing random colors "gist_ncar" coloramp. have @ colormaps here (gist_ncar 2/3 of way down): http://matplotlib.org/examples/color/colormaps_reference.html
Comments
Post a Comment