Thursday 15 May 2014

matplotlib - python: how to plot one line in different colors -


  wave = [42.0.441.978567980875397] 41.9622693388357, 41.963791391892457, ..., 41.972407378075879] lont = [- 66.706920989908909, -66.703116557977069, -66.707351643324543, ... -66.718218142021925]   

Now I have this plot as a line, different Every 10 wants to record those 'lat' and 'lont' as a period and give it a unique color. What should I do?

There are several different ways to do this "Best" approach depends mostly on you That's how many line sections you want to plot.

If you are just going to plot a fist (for example 10) line segments, then just do something like this: / p>

  plt def as uniqueish_color () NP imports imported as matplotlib.pyplot numpy: "" ". There are better ways to generate unique colors, but this is not terrible" "" return plt. Cm.gist_ncar (np.random.random ()) xy = (np.random.random ((10, 2)) - 0.5) .cumsum (axis = 0) fig, ax = plt.subplots () to begin , Stop zip (xy [: - 1], xy [1:]): x, y = zip (start, stop) ax.plot (x, y, color = uniqueish_color ()) plt.show () < / Code>  

Enter image details here

If you Some things are plotting with a million line segment, however, it will be very slow to attract, in that case, use a LineCollection example. Import MMP as NP Import PLT Import As Matplotlibkpyplot Matplotlibkcollections to LineCollection xy = (np.random.random ((1000, 2)) - 0.5). Cumsum (axis = 0) # Refresh the look so that we have a sequence: # [[(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 (sections , CMAP = plt. Cm.gist_ncar) coll.set_array (np.random.random (xy.shape [0]) ax.add_collection (coll) ax.autoscale_view () plt.show ()

Enter image details here

For both of these cases, we just Drawing from random colors are creating a "Gist_ncar" color scheme here at the colormaps (gist_ncar bottom right About 2/3):

No comments:

Post a Comment