SourceForge logo
SourceForge logo
Menu

matplotlib-users

From: Alan G I. <ala...@gm...> - 2011年07月30日 04:04:29
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
From: Tony Yu <ts...@gm...> - 2011年07月30日 16:07:34
Oops. I meant to reply to the list.
-Tony
---------- Forwarded message ----------
From: Tony Yu <ts...@gm...>
Date: Sat, Jul 30, 2011 at 10:46 AM
Subject: Re: [Matplotlib-users] parametric line color
To: Alan G Isaac <ala...@gm...>
On Sat, Jul 30, 2011 at 12:03 AM, Alan G Isaac <ala...@gm...> wrote:
> 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
>
>
Here's a function to do what you want. (I haven't tested it much so it could
be buggy)
Best,
-Tony
#----
import matplotlib.pyplot as plt
def make_color_manager(parameter_range, cmap='YlOrBr', start=0, stop=255):
 """Return color manager, which returns color based on parameter value.
 Parameters
 ----------
 parameter_range : 2-tuple
 minimum and maximum value of parameter
 cmap : str
 name of a matplotlib colormap (see matplotlib.pyplot.cm)
 start, stop: int
 limit colormap to this range (0 <= start < stop <= 255)
 """
 colormap = getattr(plt.cm, cmap)
 pmin, pmax = parameter_range
 def color_manager(val):
 """Return color based on parameter value `val`."""
 assert pmin <= val <= pmax
 val_norm = (val - pmin) * float(stop - start) / (pmax - pmin)
 idx = int(val_norm) + start
 return colormap(idx)
 return color_manager
if __name__ == '__main__':
 cm = make_color_manager((5, 10), start=100)
 plt.plot([0, 1], color=cm(5))
 plt.plot([0.5, 0.5], color=cm(7.5))
 plt.plot([1, 0], color=cm(10))
 plt.legend(('val = 5', 'val = 7.5', 'val = 10'))
 plt.show()
From: Eric F. <ef...@ha...> - 2011年07月30日 18:31:17
On 07/29/2011 06:03 PM, Alan G Isaac wrote:
> 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
>
> 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
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
From: Alan G I. <ala...@gm...> - 2011年07月31日 14:05:33
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).
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
From: Eric F. <ef...@ha...> - 2011年07月31日 18:09:37
On 07/31/2011 04:05 AM, Alan G Isaac wrote:
> 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).
>
>
>
> 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
>
> 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
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /