# Selecting a color from a given color map.

Greetings.

I have a series of lines that I would like to plot on the same axis,
but I would like to set the color of each such that the range of
colors used progresses through a given color map (e.g. the default Jet
map.) For example, if I have 7 lines, the first would use the first
most color from the Jet color map (blue.) The next line would use the
color that is 1/7 the way up the map, e.g. green or so. This would
continue until the last line was red.

pulling a specific color from it that could be handed to plot as an
rgba tuple)?

Thanks!
-dw

Hi Daniel,

You can just pass values to a colormap. If those values are evenly spaced between 0 and 1, you’ll get the result you desire. Example:

···

On Thu, Feb 16, 2012 at 4:14 PM, Daniel Welling <dantwelling@…287…> wrote:

Greetings.

I have a series of lines that I would like to plot on the same axis,

but I would like to set the color of each such that the range of

colors used progresses through a given color map (e.g. the default Jet

map.) For example, if I have 7 lines, the first would use the first

most color from the Jet color map (blue.) The next line would use the

color that is 1/7 the way up the map, e.g. green or so. This would

continue until the last line was red.

pulling a specific color from it that could be handed to plot as an

rgba tuple)?

Thanks!

-dw

#~~~~~

import numpy as np

import matplotlib.pyplot as plt

n_lines = 10

x = np.linspace(0, 10)

phase_shift = np.linspace(0, np.pi, n_lines)

color_idx = np.linspace(0, 1, n_lines)

for i, shift in zip(color_idx, phase_shift):

plt.plot(x, np.sin(x - shift), color=plt.cm.jet(i))

plt.show()

#~~~~~

Coincidentally, this past weekend, I started wrapping up random code like this into a utility package. See `cycle_cmap` in this package: https://github.com/tonysyu/mpltools/blob/master/mpltools/color.py. The package is still in the early stages, and function names could easily change, so use with caution.

Best,

-Tony