Screenshot instructions:
Windows
Mac
Red Hat Linux
Ubuntu
Click URL instructions:
Rightclick on ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)
From: Jeremy Conlin <jlconlin@gm...>  20110201 23:48:42

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 2D 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 "upsidedown" 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) 
From: Benjamin Root <ben.root@ou...>  20110202 00:01:14
Attachments:
Message as HTML

On Tue, Feb 1, 2011 at 5:48 PM, Jeremy Conlin <jlconlin@...> 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 2D 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 "upsidedown" 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. Ben Root 
From: Jeremy Conlin <jlconlin@gm...>  20110202 00:05:37

On Tue, Feb 1, 2011 at 5:00 PM, Benjamin Root <ben.root@...> wrote: > > > On Tue, Feb 1, 2011 at 5:48 PM, Jeremy Conlin <jlconlin@...> 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 2D 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 "upsidedown" 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 
From: Benjamin Root <ben.root@ou...>  20110202 00:19:28
Attachments:
Message as HTML

On Tue, Feb 1, 2011 at 6:05 PM, Jeremy Conlin <jlconlin@...> wrote: > On Tue, Feb 1, 2011 at 5:00 PM, Benjamin Root <ben.root@...> wrote: > > > > > > On Tue, Feb 1, 2011 at 5:48 PM, Jeremy Conlin <jlconlin@...> > 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 2D 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 "upsidedown" 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. That should fix it. Ben Root 
From: Paul Ivanov <pivanov314@gm...>  20110202 00:33:25
Attachments:
application/pgpsignature

Jeremy Conlin, on 20110201 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 2D 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 "upsidedown" 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 01. 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(1e15), cmap=cMap,norm=n) 
From: Eric Firing <efiring@ha...>  20110202 01:31:27

On 02/01/2011 02:18 PM, Benjamin Root wrote: > On Tue, Feb 1, 2011 at 6:05 PM, Jeremy Conlin <jlconlin@... > <mailto:jlconlin@...>> wrote: > > On Tue, Feb 1, 2011 at 5:00 PM, Benjamin Root <ben.root@... > <mailto:ben.root@...>> wrote: > > > > > > On Tue, Feb 1, 2011 at 5:48 PM, Jeremy Conlin <jlconlin@... > <mailto:jlconlin@...>> 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 2D 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 "upsidedown" 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 > > That should fix it. > > Ben Root 
From: Jeremy Conlin <jlconlin@gm...>  20110202 03:18:51

On Tue, Feb 1, 2011 at 6:31 PM, Eric Firing <efiring@...> wrote: > On 02/01/2011 02:18 PM, Benjamin Root wrote: >> On Tue, Feb 1, 2011 at 6:05 PM, Jeremy Conlin <jlconlin@... >> <mailto:jlconlin@...>> wrote: >> >> On Tue, Feb 1, 2011 at 5:00 PM, Benjamin Root <ben.root@... >> <mailto:ben.root@...>> wrote: >> > >> > >> > On Tue, Feb 1, 2011 at 5:48 PM, Jeremy Conlin <jlconlin@... >> <mailto:jlconlin@...>> 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 2D 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 "upsidedown" 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(C1E15, cmap=cMap, norm=Norm) 