parametric line color

I have many lines to plot.
Each line has an associated parameter value.
I'd like to pick the color for each line
based on its parameter value by mapping
possible parameter values to the colors
in a color map (e.g., gist_rainbow),
retrieving colors one at a time (based
on the parameter value for a line).

If two lines have the same parameter value,
I need them to have exactly the same color.

Hints?

Thanks,
Alan Isaac

I have many lines to plot.
Each line has an associated parameter value.
I'd like to pick the color for each line
based on its parameter value by mapping
possible parameter values to the colors
in a color map (e.g., gist_rainbow),
retrieving colors one at a time (based
on the parameter value for a line).

If two lines have the same parameter value,
I need them to have exactly the same color.

Hints?

Tony's reply may be the right approach for you; but an alternative, if you are plotting a set of lines all at once, is to use a LineCollection, which takes a colors kwarg. (Like all Collections, a LineCollection includes the cm.ScalarMappable mixin.) See http://matplotlib.sourceforge.net/examples/pylab_examples/line_collection2.html
but note that the lines within the collection don't have to have the same number of points.

Also, Tony's method could be modified easily to use a norm (e.g., an instance of colors.Normalize or of a subclass) in addition to a cmap.

Eric

···

On 07/29/2011 06:03 PM, Alan G Isaac wrote:

Thanks,
Alan Isaac

------------------------------------------------------------------------------
Got Input? Slashdot Needs You.
Take our quick survey online. Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

As far as I can tell, the answer is the following.
1. scale the parameter to fall in (0,1)
2. get a color (rgba tuple) as cm.gist_rainbow(scaled_param)
3. plot using that color

This works.

Cheers,
Alan Isaac

···

On 7/30/2011 12:03 AM, Alan G Isaac wrote:

I'd like to pick the color for each line
based on its parameter value by mapping
possible parameter values to the colors
in a color map (e.g., gist_rainbow),
retrieving colors one at a time (based
on the parameter value for a line).

I'd like to pick the color for each line
based on its parameter value by mapping
possible parameter values to the colors
in a color map (e.g., gist_rainbow),
retrieving colors one at a time (based
on the parameter value for a line).

As far as I can tell, the answer is the following.
1. scale the parameter to fall in (0,1)
2. get a color (rgba tuple) as cm.gist_rainbow(scaled_param)
3. plot using that color

For completeness: You can call a Colormap instance with either a float in (0,1), or equivalently with an integer in (0, N-1), where N is the number of discrete colors in the lookup table, excluding the over, under, and "bad" colors. The standard colormaps in cm are all created with N=mpl.rcParams['image.lut'], defaulting to 256, but you can make a colormap with any value of N.

Eric

···

On 07/31/2011 04:05 AM, Alan G Isaac wrote:

On 7/30/2011 12:03 AM, Alan G Isaac wrote:

This works.

Cheers,
Alan Isaac

------------------------------------------------------------------------------
Got Input? Slashdot Needs You.
Take our quick survey online. Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options