Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## [Matplotlib-users] Re: help on colorbar usage

 [Matplotlib-users] Re: help on colorbar usage From: Jeff Whitaker - 2005-08-23 19:09:31 Attachments: pylab.patch     figure.patch ```Eric Firing wrote: > >> >> Thanks Eric - I just wanted to make sure I wasn't missing some >> obvious way to do it. > > > > Jeff, > > A possible trick would be to make a very skinny subplot (using custom > sizing and positioning) and use contourf itself to make the colorbar > in it. I think this is actually the way I am doing it in Matlab. (A > colleague and I greatly customized the Matlab colorbar for this sort > of thing a long time ago.) > > Eric Eric/Phil: Here is a hack that produces colorbars with discrete contour intervals for use with contourf. It's in the form of a patch for pylab.py and figure.py (based on 0.83.2). When pylab.colorbar is called, it checks to see if the current image was created by contourf, and if so calls figure.colobar_contourf (which uses contourf instead of imshow to make the colorbar). Here's a test script: """test script for contourf colorbar""" from pylab import * def func3(x,y): return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2) dx, dy = 0.05, 0.05 X,Y = meshgrid(arange(-3.0,3.0001,dx),arange(-3.0,3.0001,dy)) Z = func3(X, Y) levels = linspace(-1.2,1.2,13) l,c = contour (X, Y, Z, levels, linewidths=0.5, colors='k') clabel(c,l,fmt='%4.2f') l,c = contourf(X, Y, Z, levels, cmap=cm.jet) colorbar(tickfmt='%4.2f',orientation='horizontal') # horiz colorbar colorbar(tickfmt='%4.2f',orientation='vertical') # vert colorbar axis([-3,3,-3,3]) show() This doesn't address the other two of Phil's requests (triangles at the ends, and constant intervals on the colorbar for non-constant contour levels), but I think it's a start. -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX : (303)497-6449 NOAA/OAR/CDC R/CDC1 Email : Jeffrey.S.Whitaker@... 325 Broadway Office : Skaggs Research Cntr 1D-124 Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg ```

 [Matplotlib-users] Re: help on colorbar usage From: Eric Firing - 2005-08-22 22:55:31 ```(from phil:) > I want to arrange the colorbar so that a very small number of colors > are displayed rather than the "continuous shading" that most of the > example plots are using. This lets viewers of the figure unambiguously > identify precisely the range of value in the filled region. (from Jeff:) > Note that that contourf is using 10 discrete colors to represent the > data, but the colorbar is showing all 256 colors in the colormap. Do > you know of any way to force the colorbar to show only those colors > that contourf uses? Phil, Jeff, Yes, the present colorbar is designed for use with images, not with contourf, and fixing it (or making a new one) is something I have wanted to do for quite a while. I just haven't gotten to it yet. Prompted by your two emails, maybe I can at least take a close look during the next week to see what it would take. It might be easy--probably is. No promises yet, though. Eric ```
 Re: [Matplotlib-users] Re: help on colorbar usage From: Eric Firing - 2005-08-23 00:06:01 ```Phil, > > As long as you are digging around in there, can I have you think about > another couple features? OK. > > An example of these features can be seen in the attached PNG figure I think you forgot to attach the figure--I didn't see it. Your description is pretty clear, though. Eric ```
 [Matplotlib-users] Re: help on colorbar usage From: Jeff Whitaker - 2005-08-23 19:09:31 Attachments: pylab.patch     figure.patch ```Eric Firing wrote: > >> >> Thanks Eric - I just wanted to make sure I wasn't missing some >> obvious way to do it. > > > > Jeff, > > A possible trick would be to make a very skinny subplot (using custom > sizing and positioning) and use contourf itself to make the colorbar > in it. I think this is actually the way I am doing it in Matlab. (A > colleague and I greatly customized the Matlab colorbar for this sort > of thing a long time ago.) > > Eric Eric/Phil: Here is a hack that produces colorbars with discrete contour intervals for use with contourf. It's in the form of a patch for pylab.py and figure.py (based on 0.83.2). When pylab.colorbar is called, it checks to see if the current image was created by contourf, and if so calls figure.colobar_contourf (which uses contourf instead of imshow to make the colorbar). Here's a test script: """test script for contourf colorbar""" from pylab import * def func3(x,y): return (1- x/2 + x**5 + y**3)*exp(-x**2-y**2) dx, dy = 0.05, 0.05 X,Y = meshgrid(arange(-3.0,3.0001,dx),arange(-3.0,3.0001,dy)) Z = func3(X, Y) levels = linspace(-1.2,1.2,13) l,c = contour (X, Y, Z, levels, linewidths=0.5, colors='k') clabel(c,l,fmt='%4.2f') l,c = contourf(X, Y, Z, levels, cmap=cm.jet) colorbar(tickfmt='%4.2f',orientation='horizontal') # horiz colorbar colorbar(tickfmt='%4.2f',orientation='vertical') # vert colorbar axis([-3,3,-3,3]) show() This doesn't address the other two of Phil's requests (triangles at the ends, and constant intervals on the colorbar for non-constant contour levels), but I think it's a start. -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX : (303)497-6449 NOAA/OAR/CDC R/CDC1 Email : Jeffrey.S.Whitaker@... 325 Broadway Office : Skaggs Research Cntr 1D-124 Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg ```
 Re: [Matplotlib-users] Re: help on colorbar usage From: Phil Rasch - 2005-08-22 23:44:59 ```On Mon, Aug 22, 2005 at 12:55:16PM -1000, Eric Firing wrote: > (from phil:) .... stuff deleted ...... > > Phil, Jeff, > > Yes, the present colorbar is designed for use with images, not with > contourf, and fixing it (or making a new one) is something I have wanted > to do for quite a while. I just haven't gotten to it yet. Prompted by > your two emails, maybe I can at least take a close look during the next > week to see what it would take. It might be easy--probably is. No > promises yet, though. > > Eric Thanks Eric, I also believe it should be easy to do. But it is better done by somebody more familiar with matplotlib and python than I. I have written such codes for other languages but need more practice before I could do it elegantly in python. As long as you are digging around in there, can I have you think about another couple features? 1) The best colorbars in my mind have "triangles" at the endpoints that indicate the color for the region higher than the highest contour, and lower than the lowest contours. This allows one to label only the meaningful boundaries and not specify how much above or below those regions. 2) I frequently need to set contour intervals (the filled region boundaries) to be approximately logarithmic. But I dont want to have these filled regions occupy a fraction of the colorbar proportional to their fraction of the total interval. I want each region to be equal area on the colorbar. An example of these features can be seen in the attached PNG figure that I created in Yorick with a colorbar code I wrote. I havent tried for beauty, but these figure are OK for for working plots. You can see the point for the unequal contour intervals in the difference plot at the bottom. For the codes I wrote this necessitated supplying arguments to the colorbar function like.... colorbar(levs, colors) where levels was an N element array, and colors was an N+1 element list containing color info for each filled region. Thanks for listening. Phil ```
 Re: [Matplotlib-users] Re: help on colorbar usage From: Eric Firing - 2005-08-23 00:03:44 ```Phil Rasch wrote: > On Mon, Aug 22, 2005 at 12:55:16PM -1000, Eric Firing wrote: > >>(from phil:) > > .... stuff deleted ...... > >>Phil, Jeff, >> >>Yes, the present colorbar is designed for use with images, not with >>contourf, and fixing it (or making a new one) is something I have wanted >>to do for quite a while. I just haven't gotten to it yet. Prompted by >>your two emails, maybe I can at least take a close look during the next >>week to see what it would take. It might be easy--probably is. No >>promises yet, though. >> >>Eric > > > Thanks Eric, > > I also believe it should be easy to do. But it is better done by > somebody more familiar with matplotlib and python than I. I have > written such codes for other languages but need more practice before > I could do it elegantly in python. > > As long as you are digging around in there, can I have you think about > another couple features? > > 1) The best colorbars in my mind have "triangles" at the endpoints > that indicate the color for the region higher than the highest > contour, and lower than the lowest contours. This allows one to label > only the meaningful boundaries and not specify how much above or below > those regions. > > 2) I frequently need to set contour intervals (the filled region > boundaries) to be approximately logarithmic. But I dont want to have > these filled regions occupy a fraction of the colorbar proportional to > their fraction of the total interval. I want each region to be equal > area on the colorbar. > > An example of these features can be seen in the attached PNG figure > that I created in Yorick with a colorbar code I wrote. I havent tried > for beauty, but these figure are OK for for working plots. You can see > the point for the unequal contour intervals in the difference plot at > the bottom. > > For the codes I wrote this necessitated supplying arguments to the > colorbar function like.... > > colorbar(levs, colors) > where levels was an N element array, and colors was an N+1 element > list containing color info for each filled region. > > Thanks for listening. > > Phil > ```
 Re: [Matplotlib-users] Re: help on colorbar usage From: Phil Rasch - 2005-08-23 02:02:59 Attachments: example.png ```Just for completness I will attach the figure this time. Phil On Mon, Aug 22, 2005 at 02:02:57PM -1000, Eric Firing wrote: > Phil Rasch wrote: > >On Mon, Aug 22, 2005 at 12:55:16PM -1000, Eric Firing wrote: > > > >>(from phil:) > > > >.... stuff deleted ...... > > > >>Phil, Jeff, > >> > >>Yes, the present colorbar is designed for use with images, not with > >>contourf, and fixing it (or making a new one) is something I have wanted > >>to do for quite a while. I just haven't gotten to it yet. Prompted by > >>your two emails, maybe I can at least take a close look during the next > >>week to see what it would take. It might be easy--probably is. No > >>promises yet, though. > >> > >>Eric > > > > > >Thanks Eric, > > > >I also believe it should be easy to do. But it is better done by > >somebody more familiar with matplotlib and python than I. I have > >written such codes for other languages but need more practice before > >I could do it elegantly in python. > > > >As long as you are digging around in there, can I have you think about > >another couple features? > > > >1) The best colorbars in my mind have "triangles" at the endpoints > >that indicate the color for the region higher than the highest > >contour, and lower than the lowest contours. This allows one to label > >only the meaningful boundaries and not specify how much above or below > >those regions. > > > >2) I frequently need to set contour intervals (the filled region > >boundaries) to be approximately logarithmic. But I dont want to have > >these filled regions occupy a fraction of the colorbar proportional to > >their fraction of the total interval. I want each region to be equal > >area on the colorbar. > > > >An example of these features can be seen in the attached PNG figure > >that I created in Yorick with a colorbar code I wrote. I havent tried > >for beauty, but these figure are OK for for working plots. You can see > >the point for the unequal contour intervals in the difference plot at > >the bottom. > > > >For the codes I wrote this necessitated supplying arguments to the > >colorbar function like.... > > > >colorbar(levs, colors) > >where levels was an N element array, and colors was an N+1 element > >list containing color info for each filled region. > > > >Thanks for listening. > > > >Phil > > -- Phil Rasch, Climate Modeling Section, National Center for Atmospheric Research Mail --> P.O. Box 3000, Boulder CO 80307 Shipping --> 1850 Table Mesa Dr, Boulder, CO 80305 email: pjr@..., Web: http://www.cgd.ucar.edu/cms/pjr Phone: 303-497-1368, FAX: 303-497-1324 ```