Jeremy Conlin, on 2011-02-01 16:48,  wrote:
> I'm trying to create a custom colormap used with pcolormesh, but the
> results seem inconsistent to me.  I want the following colors
> 
> -3 < x <= -2 ----- Black
> -2 < x <= -1 ----- Blue
> -1 < x <= 0  ----- Yellow
>  0 < x <= 1  ----- Green
>  1 < x <= inf ----- Red
> 
> A minimal example is copied below.  I have a 2-D array that looks like:
> 
>  -1,    6,  2.5
> 1.3,  -2,  4/3
> 2.5,   6,  0
> 
> I want to get a pcolormesh that looks like
> 
> R R Y
> R K R
> B R R
> 
> But instead I get:
> 
> Y R B
> Y K Y
> K R Y
> 
> I recognize that the pcolormesh is plotted "upside-down" from how the
> matrix is printed.  I apparently don't understand how to use a custom
> colormap.  I have tried to follow the example here:
> 
> http://matplotlib.sourceforge.net/examples/api/colorbar_only.html
> 
> but haven't been too successful.  It seems like there is a
> normalization going on that I can't seem to track down.  Can anyone
> see what is wrong?
> 
> Thanks,
> Jeremy
> 
> 
> import numpy
> import matplotlib.pyplot as pyplot
> import matplotlib.colors
> 
> C = numpy.array([[-1,6,2.5],[4/3., -2, 4/3.],[2.5,6,0.0]],dtype=float)
> 
> cMap = matplotlib.colors.ListedColormap(['k', 'b', 'y', 'g',         'r'])
> Bounds = [-3.0, -2.0, -1.0, 0.0, 1.0, numpy.inf]
> 
> # Plot
> Fig = pyplot.figure()
> pyplot.pcolormesh(C, cmap=cMap)

Hi Jeremy,

you're right, matplotlib expects colors to be in the range 0-1.
I've added the appropriate normalization below. I also had to
subtract a small number from C to adjust for your specification
of the desired intervals being closed on the upper bound, because
the default makes lower bound closed. In other words, the default
is to treat the bounds as -3 <= x < -2 for black, in your case,
instead of -3 < x <= -2 as you wanted it.


  # R R Y
  # R K R
  # B R R
  n = mpl.colors.normalize(-3,2)
  pyplot.pcolormesh(C-(1e-15), cmap=cMap,norm=n)

Attachment: signature.asc
Description: Digital signature

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to