## Re: [Matplotlib-users] incremental colors for lines

 Re: [Matplotlib-users] incremental colors for lines From: Daniel Mader - 2011-05-10 07:44:37 ```Hi, I like this, too. However, I don't understand why it works at all. Usually, when I apply a colormap, I need to take care about the scaling myself, i.e. divide the range up into the number of elements to plot: import pylab as pl import matplotlib.cm as cm xval = pl.arange(0, 20, 0.2) n = 256 for i in range(n): # pl.plot(xval, pl.sin(xval)+i, c=cm.hot(i), lw=5) pl.plot(xval, pl.sin(xval)+i, c=cm.hot(1.*i/n), lw=5) Can anyone tell me why this is not necessary here but essential for example here: for i,infile in enumerate(infiles): ## title for plot tname = os.path.splitext(infile)[0] ## read data f = FileHelpers.BlockedFile(infile) alldata = scipy.array([[],[]]) for ii in ['+', '2', 'x', '1']: # use for markers, too # for ii in [4,3,2,1]: # use for markers, too try: f.next_block() data = scipy.loadtxt(f).T alldata = scipy.concatenate((alldata, data), axis=1) # ax.plot(data[0],data[1], '%s'%ii, color=cm.hot(1.*i/len(infiles)), mew=1.5 ) ax.plot(data[0],data[1], '%s'%ii, c=cm.hot(i), mew=1.5 ) except Exception, e: print e break Thanks in advance, Daniel >> I have found a simple and better way. One can chose from colors from a >> color >> map: >> >> >>import pylab as pl >> >>import matplotlib.cm as cm >> >>xval = pl.arange(0, 20, 0.2) >> >>for i in range(256): >>     ...  pl.plot(xval, pl.sin(xval)+i, c=cm.hot(i), lw=5) >> >> This one if, for instance, picking from a color map called "hot". If one >> wants to the colors to fade away, or darken, the "alpha" option can be >> utilized or another color map in which colors darken or fade into another >> color. >> >> There is no need for a long sophisticated script. ```

 [Matplotlib-users] incremental colors for lines From: Pythonified - 2011-05-05 01:51:24 Attachments: Message as HTML ```I have been trying to assign different colors for each line I plot, where the colors are incrementally darkened (or lightened), or selected from a colorbar (e.g. rainbow). Any ideas? -- View this message in context: http://old.nabble.com/incremental-colors-for-lines-tp31546719p31546719.html Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] incremental colors for lines From: Tony Yu - 2011-05-06 17:34:08 Attachments: Message as HTML ```(Sorry for sending this twice, Pythonified, but I forgot to copy the list) On Wed, May 4, 2011 at 9:51 PM, Pythonified wrote: > I have been trying to assign different colors for each line I plot, where > the colors are incrementally darkened (or lightened), or selected from a > colorbar (e.g. rainbow). Any ideas? > I posted some code awhile back to do what you're looking for (see: http://old.nabble.com/Where-to-post-examples-%28specifically,-one-that-may-be-useful-for-time-evolution-plots%29-td23901837.html ). I'm copying the code below again because it's evolved a bit (I really need to start posting code to github). Anyway, if you copy the attached code to a module, you should be able to call the "cycle_cmap" function to change the cmap globally, or the "cycle_cmap_axes" function to create an axes with the specified cmap. Best, -Tony #---- start of code import matplotlib.pyplot as plt import numpy as np # reverse these colormaps so that it goes from light to dark REVERSE_CMAP = ['summer', 'autumn', 'winter', 'spring', 'copper'] # clip some colormaps so the colors aren't too light CMAP_RANGE = dict(gray={'start':200, 'stop':0}, Blues={'start':60, 'stop':255}, Oranges={'start':100, 'stop':255}, OrRd={'start':60, 'stop':255}, BuGn={'start':60, 'stop':255}, PuRd={'start':60, 'stop':255}, YlGn={'start':60, 'stop':255}, YlGnBu={'start':60, 'stop':255}, YlOrBr={'start':60, 'stop':255}, YlOrRd={'start':60, 'stop':255}, hot={'start':230, 'stop':0}, bone={'start':200, 'stop':0}, pink={'start':160, 'stop':0}) def cmap_intervals(length=50, cmap='YlOrBr', start=None, stop=None): """Return evenly spaced intervals of a given colormap `cmap`. Colormaps listed in REVERSE_CMAP will be cycled in reverse order. Certain colormaps have pre-specified color ranges in CMAP_RANGE. These module variables ensure that colors cycle from light to dark and light colors are not too close to white. Parameters ---------- length : int the number of colors used before cycling back to first color. When `length` is large (> ~10), it is difficult to distinguish between successive lines because successive colors are very similar. cmap : str name of a matplotlib colormap (see matplotlib.pyplot.cm) """ cm = getattr(plt.cm, cmap) crange = CMAP_RANGE.get(cmap, dict(start=0, stop=255)) if cmap in REVERSE_CMAP: crange = dict(start=crange['stop'], stop=crange['start']) if start is not None: crange['start'] = start if stop is not None: crange['stop'] = stop if length > abs(crange['start'] - crange['stop']): print ('Warning: the input length is greater than the number of ' + 'colors in the colormap; some colors will be repeated') idx = np.linspace(crange['start'], crange['stop'], length).astype(np.int ) return cm(idx) def cycle_cmap(length=50, cmap='YlOrBr', start=None, stop=None): """Set default color cycle of matplotlib to a given colormap `cmap`. The default color cycle of matplotlib is set to evenly distribute colors in color cycle over specified colormap. Note: this function must be called before *any* plot commands because it changes the default color cycle. See ``cmap_intervals`` for input details. """ color_cycle = cmap_intervals(length, cmap, start, stop) # set_default_color_cycle doesn't play nice with numpy arrays plt.rc('axes', color_cycle=color_cycle.tolist()) def cycle_cmap_axes(length=50, cmap='YlOrBr', start=None, stop=None): """Return axes with color cycle set to a given colormap `cmap`. The color cycle of the axes is set to evenly distribute colors in color cycle over specified colormap. See ``cmap_intervals`` for input details. """ color_cycle = cmap_intervals(length, cmap, start, stop) # set_default_color_cycle doesn't play nice with numpy arrays ax = plt.gca() ax.set_color_cycle(color_cycle) return ax if __name__ == '__main__': n_lines = 10 x = np.linspace(0, n_lines) # change the global cmap cycle_cmap(n_lines, 'Oranges') for shift in np.linspace(0, np.pi, n_lines): plt.plot(x, np.sin(x - shift)) plt.figure() # create an axes that is set to desired cmap ax = cycle_cmap_axes(n_lines, 'Blues') for shift in np.linspace(0, np.pi, n_lines): ax.plot(x, np.sin(x - shift)) plt.show() ```
 Re: [Matplotlib-users] incremental colors for lines From: Pythonified - 2011-05-09 23:11:09 Attachments: Message as HTML ``` Pythonified wrote: > > I have been trying to assign different colors for each line I plot, where > the colors are incrementally darkened (or lightened), or selected from a > colorbar (e.g. rainbow). > > Any ideas? > I have found a simple and better way. One can chose from colors from a color map: >>import pylab as pl >>import matplotlib.cm as cm >>xval = pl.arange(0, 20, 0.2) >>for i in range(256): ... pl.plot(xval, pl.sin(xval)+i, c=cm.hot(i), lw=5) This one if, for instance, picking from a color map called "hot". If one wants to the colors to fade away, or darken, the "alpha" option can be utilized or another color map in which colors darken or fade into another color. There is no need for a long sophisticated script. Enjoy, Pythonified -- View this message in context: http://old.nabble.com/incremental-colors-for-lines-tp31546719p31581404.html Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] incremental colors for lines From: Gökhan Sever - 2011-05-10 05:20:23 Attachments: Message as HTML ```On Mon, May 9, 2011 at 5:11 PM, Pythonified wrote: > > > Pythonified wrote: > > > > I have been trying to assign different colors for each line I plot, where > > the colors are incrementally darkened (or lightened), or selected from a > > colorbar (e.g. rainbow). > > > > Any ideas? > > > > I have found a simple and better way. One can chose from colors from a > color > map: > > >>import pylab as pl > >>import matplotlib.cm as cm > >>xval = pl.arange(0, 20, 0.2) > >>for i in range(256): > ... pl.plot(xval, pl.sin(xval)+i, c=cm.hot(i), lw=5) > > This one if, for instance, picking from a color map called "hot". If one > wants to the colors to fade away, or darken, the "alpha" option can be > utilized or another color map in which colors darken or fade into another > color. > > There is no need for a long sophisticated script. > > Enjoy, > Pythonified > Nice trick. This can go into the gallery or somewhere else in scipy cookbook. -- Gökhan ```
 Re: [Matplotlib-users] incremental colors for lines From: Daniel Mader - 2011-05-10 07:44:37 ```Hi, I like this, too. However, I don't understand why it works at all. Usually, when I apply a colormap, I need to take care about the scaling myself, i.e. divide the range up into the number of elements to plot: import pylab as pl import matplotlib.cm as cm xval = pl.arange(0, 20, 0.2) n = 256 for i in range(n): # pl.plot(xval, pl.sin(xval)+i, c=cm.hot(i), lw=5) pl.plot(xval, pl.sin(xval)+i, c=cm.hot(1.*i/n), lw=5) Can anyone tell me why this is not necessary here but essential for example here: for i,infile in enumerate(infiles): ## title for plot tname = os.path.splitext(infile)[0] ## read data f = FileHelpers.BlockedFile(infile) alldata = scipy.array([[],[]]) for ii in ['+', '2', 'x', '1']: # use for markers, too # for ii in [4,3,2,1]: # use for markers, too try: f.next_block() data = scipy.loadtxt(f).T alldata = scipy.concatenate((alldata, data), axis=1) # ax.plot(data[0],data[1], '%s'%ii, color=cm.hot(1.*i/len(infiles)), mew=1.5 ) ax.plot(data[0],data[1], '%s'%ii, c=cm.hot(i), mew=1.5 ) except Exception, e: print e break Thanks in advance, Daniel >> I have found a simple and better way. One can chose from colors from a >> color >> map: >> >> >>import pylab as pl >> >>import matplotlib.cm as cm >> >>xval = pl.arange(0, 20, 0.2) >> >>for i in range(256): >>     ...  pl.plot(xval, pl.sin(xval)+i, c=cm.hot(i), lw=5) >> >> This one if, for instance, picking from a color map called "hot". If one >> wants to the colors to fade away, or darken, the "alpha" option can be >> utilized or another color map in which colors darken or fade into another >> color. >> >> There is no need for a long sophisticated script. ```