question about setting colors of lines using colormap

Hi all,

i would like to set the colors of the lines i plot (using the plot function) to go from red to blue, in evenly spaced interval. that is, imagine a color map from red to green, where i plot n-many lines, each receiving a color from this color map, starting at the red end and going to green.

the docs say how to set the color cycle of lines set by plot, using:

matplotlib.axes.set_default_color_cycle([‘r’, ‘y’, ‘g’, ‘b’])

but this only allows me to use named colors, and here i am looking to use shades from red to green.
my question is: first, how can i generate N evenly spaced colors from the red spectrum to the green spectrum? and two, how can i make it so plot uses these colors for its line plots?

thanks very much.

Not too long ago, I posted an example of this to the list. The code near the bottom of that thread is a little more general than the one at the top and shows, three different ways to cycle through the colors of a colormap.

Hope that helps,

-Tony

···

On Jul 14, 2009, at 9:51 AM, per freem wrote:

Hi all,

i would like to set the colors of the lines i plot (using the plot function) to go from red to blue, in evenly spaced interval. that is, imagine a color map from red to green, where i plot n-many lines, each receiving a color from this color map, starting at the red end and going to green.

the docs say how to set the color cycle of lines set by plot, using:

matplotlib.axes.set_default_color_cycle([‘r’, ‘y’, ‘g’, ‘b’])

but this only allows me to use named colors, and here i am looking to use shades from red to green.
my question is: first, how can i generate N evenly spaced colors from the red spectrum to the green spectrum? and two, how can i make it so plot uses these colors for its line plots?

thanks very much.

Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World™ will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize
details at: http://p.sf.net/sfu/Challenge_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Tony,

thanks for the pointer. that code does not run for me, it generates the following error:

ttributeError Traceback (most recent call last)

color_cycle.py in ()
63 if name == ‘main’:
64 n_lines = 10
—> 65 cycle_cmap(length=n_lines)
66 x = np.linspace(0, 10)
67 for shift in np.linspace(0, np.pi, n_lines):

color_cycle.py in cycle_cmap(cmap, length)
59 idx = np.linspace(crange[‘start’], crange[‘stop’], length).astype(np.int)
60 colors = cm(idx)
—> 61 mpl.axes.set_default_color_cycle(colors.tolist())
62
63 if name == ‘main’:

/Library/Python/2.5/site-packages/matplotlib/axes.pyc in set_default_color_cycle(clist)
113 “”"
114 _process_plot_var_args.defaultColors = clist[:]
–> 115 rcParams[‘lines.color’] = clist[0]
116
117 class _process_plot_var_args:

/Library/Python/2.5/site-packages/matplotlib/init.pyc in setitem(self, key, val)
603 instead.’% (key, alt))
604 key = alt
–> 605 cval = self.validatekey
606 dict.setitem(self, key, cval)
607 except KeyError:

/Library/Python/2.5/site-packages/matplotlib/rcsetup.pyc in validate_color(s)
160 def validate_color(s):
161 ‘return a valid color arg’
–> 162 if s.lower() == ‘none’:
163 return ‘None’
164 if is_color_like(s):

AttributeError: ‘list’ object has no attribute ‘lower’
WARNING: Failure executing file: <color_cycle.py>

any idea what might be wrong?

···

On Tue, Jul 14, 2009 at 10:14 AM, Tony S Yu <tonyyu@…1166…> wrote:

Not too long ago, I posted an example of this to the list. The code near the bottom of that thread is a little more general than the one at the top and shows, three different ways to cycle through the colors of a colormap.

Hope that helps,

-Tony

On Jul 14, 2009, at 9:51 AM, per freem wrote:

Hi all,

i would like to set the colors of the lines i plot (using the plot function) to go from red to blue, in evenly spaced interval. that is, imagine a color map from red to green, where i plot n-many lines, each receiving a color from this color map, starting at the red end and going to green.

the docs say how to set the color cycle of lines set by plot, using:

matplotlib.axes.set_default_color_cycle([‘r’, ‘y’, ‘g’, ‘b’])

but this only allows me to use named colors, and here i am looking to use shades from red to green.

my question is: first, how can i generate N evenly spaced colors from the red spectrum to the green spectrum? and two, how can i make it so plot uses these colors for its line plots?

thanks very much.


Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World™ will have

the opportunity to enter the BlackBerry Developer Challenge. See full prize
details at: http://p.sf.net/sfu/Challenge_______________________________________________

Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Tony,

thanks for the pointer. that code does not run for me, it generates the following error:

ttributeError Traceback (most recent call last)

/Library/Python/2.5/site-packages/matplotlib/rcsetup.pyc in validate_color(s)
160 def validate_color(s):
161 ‘return a valid color arg’
–> 162 if s.lower() == ‘none’:
163 return ‘None’
164 if is_color_like(s):

AttributeError: ‘list’ object has no attribute ‘lower’
WARNING: Failure executing file: <color_cycle.py>

any idea what might be wrong?

Hmm, it looks like there was a fix added a couple of months ago (svn revision 7164). If you’re on trunk, then updating it should fix this problem. If you’re not on trunk: I don’t think any official releases have occurred since this fix, so I don’t think updating to the last official release will help.

In any case, I think this error only affects the cycle_cmap function in the code (calls to cmap_intervals should work fine). If you comment out the 4 lines of code after the comment “Change the default color cycle”, I believe everything should work fine.

-Tony

···

On Jul 14, 2009, at 3:12 PM, per freem wrote:

I also recently posted an example that may do something similar to what you
are after, you can have a look here:
http://www.nabble.com/contribute-to-gallery--Or%2C-just-advice-on-changing-colors-automagically…-td24419101.html#a24427781

per freem-2 wrote:

···

Hi all,

i would like to set the colors of the lines i plot (using the plot
function)
to go from red to blue, in evenly spaced interval. that is, imagine a
color
map from red to green, where i plot n-many lines, each receiving a color
from this color map, starting at the red end and going to green.

the docs say how to set the color cycle of lines set by plot, using:

matplotlib.axes.set_default_color_cycle(['r', 'y', 'g', 'b'])

but this only allows me to use named colors, and here i am looking to use
shades from red to green.
my question is: first, how can i generate N evenly spaced colors from the
red spectrum to the green spectrum? and two, how can i make it so plot
uses
these colors for its line plots?

thanks very much.

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full
prize
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
View this message in context: http://www.nabble.com/question-about-setting-colors-of-lines-using-colormap-tp24479842p24498248.html
Sent from the matplotlib - users mailing list archive at Nabble.com.