On Tue, Feb 1, 2011 at 6:31 PM, Eric Firing <efir...@hawaii.edu> wrote: > On 02/01/2011 02:18 PM, Benjamin Root wrote: >> On Tue, Feb 1, 2011 at 6:05 PM, Jeremy Conlin <jlcon...@gmail.com >> <mailto:jlcon...@gmail.com>> wrote: >> >> On Tue, Feb 1, 2011 at 5:00 PM, Benjamin Root <ben.r...@ou.edu >> <mailto:ben.r...@ou.edu>> wrote: >> > >> > >> > On Tue, Feb 1, 2011 at 5:48 PM, Jeremy Conlin <jlcon...@gmail.com >> <mailto:jlcon...@gmail.com>> 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) >> >> >> > >> > Have you given imshow() a try? The pcolor() and family are >> really meant for >> > more general domain specifications. imshow() is about as basic >> as one can >> > get for producing an image that shows the colors for particular >> values. >> > matshow() also does something similar and doesn't interpolate between >> > points. >> > >> > I don't know if it would fix your problem, but it should be a >> good start. >> >> I just tried both imshow and matshow and they gave the same output, >> but the plot was rotated -90º. I don't care so much about how it is >> oriented, but I do care about consistency, i.e. -1 should be plotted >> as blue, but is instead black. I could also accept -1 as yellow since >> -1 is on the boundary. pcolor, imshow, and matshow all show the same >> inconsistency. >> >> Jeremy >> >> >> I think I just figured out what is wrong. In your code, you create a >> ListedColormap, but you don't assign a Norm object. So, when you call >> pcolor or whatever, it will use the default norm using the range of >> input values. I see you created a list of boundaries called Bounds, but >> you don't do anything with it. >> >> I believe you want to first make a BoundaryNorm object using Bounds and >> pass that object to the ListedColormap using the norm keyword. > > Not to the ListedColormap, but to the imshow or whatever. Here is an > example of BoundaryNorm: > > http://matplotlib.sourceforge.net/examples/pylab_examples/image_masked.html > > And here is another, using a BoundaryNorm with a ListedColormap: > > http://matplotlib.sourceforge.net/examples/pylab_examples/multicolored_line.html > > (It is the first of the two line plots.) > > Note that you need to instantiate it with the number of colors in your > colormap. > > Eric
Thanks everyone for there help. You were right, it was a normalization problem. I added a normalization and now I get what I want (see final code below). 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] Norm = matplotlib.colors.BoundaryNorm(Bounds, cMap.N) # Plot Fig = pyplot.figure() pyplot.pcolormesh(C-1E-15, cmap=cMap, norm=Norm) ------------------------------------------------------------------------------ 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