On 2012/11/19 11:42 AM, TP wrote:
> Hi everybody,
>
> I have a problem with LinearSegmentedColormap.
> In the example below (see PS), I make a colormap, and use it to plot an
> EllipseCollection. My plot is parameterized by a quantity that I have named
> "large_value". For large_value equal to 257, a blue point is obtained at
> (x=0.3, y=0.4). But for large_value equal to 258, it becomes black.
>
> This is because of the way LinearSegmentedColormap is working. It has a
> parameter N which allows to set the "number of colors":
>
> http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap
>
> It is 256 by default, so if I increase N to a greater value, the point remains
> blue for large_value equal to 258.
>
> Now, my real plot (not this dummy example) is such that I need N to be very
> large so as to obtain the right colors on my plot, although very few colors
> are used at the end.
> However, when N is too large, the plot becomes very slow, and a lot of memory
> is used; I think because an array is probably built with this size, although
> in theory there is no need to construct such a complete array.
>
> Is there an easy workaround, or have I to study and modify the matplotlib code
> myself?

It is not entirely clear to me what you are trying to do, but it sounds 
like increasing N is not the right way to do it. Three things might help 
you find a better way:

1) The colormap is intended to work with a norm that handles the 
translation from your data numbers to the 0-1.0 range used to select 
values from the colormap (with exceptions--see below).  You can choose a 
non-default norm, you can write your own, or you can set the parameters 
(vmin, vmax) of the standard linear norm.

2) By creating a colormap and calling its set_under, set_over, and 
set_invalid methods, you can control the colors assigned to data values 
that your norm maps respectively to negative numbers, numbers greater 
than 1, and masked values.  See 
http://matplotlib.org/examples/pylab_examples/contourf_demo.html for an 
example of using set_under and set_over.  See 
http://matplotlib.org/examples/pylab_examples/image_masked.html for 
another example, and for an example of controlling the norm parameters 
or using an alternative norm.

3) It is also possible to index directly into the colormap if you use a 
norm that returns an integer data type.  An example of such is the 
BoundaryNorm. 
http://matplotlib.org/examples/pylab_examples/multicolored_line.html

If all you need is a single assignment of a color to a "large value", 
then using the set_over method will take care of it.

Eric

>
> Thanks,
>
> TP
>
> PS: Here is the test code:
> ##################
> from pylab import *
> from matplotlib.colors import LinearSegmentedColormap
> from matplotlib.collections import CircleCollection
>
> ioff()
> large_value = 257 # blue below this value
> #large_value = 258 # black above this value
> N = 1e5 # 256 by default
>
> cdict = { 'blue': [(0.0, 0.0, 0.0),
>                      (2*1/large_value, 1, 1)
>                      , (1.0, 1.0, 1.0)]
>                      ,  'green': [(0.0, 0.0, 0.0),
>                          (2*1/large_value, 0, 0)
>                          , (1.0, 1.0, 1.0)]
>                      , 'red': [(0.0, 0.0, 0.0),
>                              (2*1/large_value, 0, 0),
>                              (1.0, 1.0, 1.0)] }
>
> measures= array([[ 0.2,   0.3,   1],
>         [  0.3,   0.4,   2],
>         [  0.5,   0.6,   large_value]])
>
> cmap = LinearSegmentedColormap( "cmap foobar"
>          , cdict
>         # , N= N )
>          )
>
> fig = figure()
> axes = fig.add_subplot(111)
> ec = CircleCollection( [80]
>          , offsets = measures[:,:2]
>          , transOffset = axes.transData
>          )
>
> ec.set_array( measures[:,2] )
> ec.set_cmap( cmap )
> axes.add_collection( ec )
>
> show()
> ##################
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>


------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to