colorbar patch

Here's a patch against 0.83.2 that modifies colorbar to use contourf instead of imshow (so that only those colors used by contourf are shown on the colorbar). It also adds a few keyword options to colorbar to 1) allow for linear spacing of colors on the colorbar (even if the contour levels are not linear), and 2) draw lines on the colorbar between the individual colors.

Eric and Jim: I think you have both been hacking on colorbar, perhaps we could merge your changes into this patch? Otherwise, if no one has any objections, I'll commit this to CVS later this week.

Attached also is a test script to exercise the new colorbar features. The default behavior should be the same as before.


colorbar.patch (6.98 KB) (1.04 KB)


Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/CDC R/CDC1 Email : Jeffrey.S.Whitaker@...236...
325 Broadway Office : Skaggs Research Cntr 1D-124
Boulder, CO, USA 80303-3328 Web :

Jeff, John,

I have committed to CVS a set of changes that fix the recent bug in contourf when called with the colors kwarg, and that I hope will make and everything that uses it easier to understand and use. The CHANGELOG entry is:

2005-09-25 modified to use a single ContourSet class
            that handles filled contours, line contours, and labels;
            added keyword arg (clip_ends) to contourf.
            Colorbar modified to work with new ContourSet object;
            if the ContourSet has lines rather than polygons, the
            colorbar will follow suit. Fixed a bug introduced in
            0.84, in which contourf(...,colors=...) was broken - EF

and the API_CHANGES entry is:

     Changed the internal structure of to a more OO style.
     Calls to contour or contourf in or now return
     a ContourSet object which contains references to the
     LineCollections or PolyCollections created by the call,
     as well as the configuration variables that were used.
     The ContourSet object is a "mappable" if a colormap was used.

     Added a clip_ends kwarg to contourf. From the docstring:
              * clip_ends = True
                If False, the limits for color scaling are set to the
                minimum and maximum contour levels.
                True (default) clips the scaling limits. Example:
                if the contour boundaries are V = [-100, 2, 1, 0, 1, 2, 100],
                then the scaling limits will be [-100, 100] if clip_ends
                is False, and [-3, 3] if clip_ends is True.

     Changed Figure.colorbar():
         kw argument order changed;
         if mappable arg is a non-filled ContourSet, colorbar() shows
                 lines instead of polygons.
         if mappable arg is a filled ContourSet with clip_ends=True,
                 the endpoints are not labelled, so as to give the
                 correct impression of open-endedness.

     Changed LineCollection.get_linewidths to get_linewidth, for

basemap.diff_Nuw (5.27 KB)



The change in contour and contourf output breaks basemap. I have made the necessary changes in my working copy; the diff against CVS is attached. If it is OK with you, Jeff, I will commit this change. Or if you would rather make the modifications yourself, let me know.

What all this implies is that the next time there is a release of mpl there needs to be a simultaneous basemap release, and users will need to be warned to update both.