norm keyword to contourf

Hello,

I’m having trouble scaling the z-values for a filled contour plot. I would like to produce a series of plots like:

plot.contourf(x, y, z, 20, cmap=matplotlib.cm.RdBu_r)

but I would like there to be an absolute correspondence between a given z-value and a given color in the colormap no matter what the range of z-values for a particular plot. I thought the way to do this would be to use the norm keyword like:

normFunction = matplotlib.colors.normalize(vmin=minValue, maxValue)

plot.contourf(x, y, z, 20, norm=normFunction, cmap=matplotlib.cm.RdBu_r)

but this gives the error:

File “/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/axes.py”, line 4054, in contourf

return ContourSet(self, *args, **kwargs)

File “/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/contour.py”, line 456, in init

for level, level_upper, color in zip(lowers, uppers, self.tcolors):

AttributeError: ContourSet instance has no attribute ‘tcolors’

Any suggestions?

Thanks,

Mike

Michael,

You have found a big bug that I introduced recently. Here is a patch, which I committed to svn: (watch out for line-breaking by the mailer)

--- lib/matplotlib/contour.py (revision 2899)
+++ lib/matplotlib/contour.py (working copy)
@@ -680,7 +680,12 @@
          if self.extend in ('both', 'max', 'min'):
              self.norm.clip = False
          self.set_array(self.layers)
- # self.tcolors will be set by the "changed" method
+ # self.tcolors are set by the "changed" method,
+ # but need to be set here also
+ # until some refactoring is done.
+ tcolors = [ (tuple(rgba),) for rgba in
+ self.to_rgba(self.cvalues, alpha=self.alpha)]
+ self.tcolors = tcolors

      def _process_linewidths(self):
          linewidths = self.linewidths

As the comments indicate, I think I will want to make more extensive changes, but this removes the immediate problem.

To get the result you want, however, I think that your approach using the norm will not work; maybe it should, but right now it doesn't. Instead, simply specify the contour levels that you want, like this:

contourf(x, y, z, linspace(minVal, maxVal, 20), cmap=matplotlib.cm.RdBu_r)

The option you were using, specifying only the number of contours, is designed for quick-and-dirty automatic contouring, not for fine control.

Eric

Michael Galloy wrote:

···

Hello,

I'm having trouble scaling the z-values for a filled contour plot. I would like to produce a series of plots like:

plot.contourf(x, y, z, 20, cmap=matplotlib.cm.RdBu_r)

but I would like there to be an absolute correspondence between a given z-value and a given color in the colormap no matter what the range of z-values for a particular plot. I thought the way to do this would be to use the norm keyword like:

normFunction = matplotlib.colors.normalize(vmin=minValue, maxValue) plot.contourf(x, y, z, 20, norm=normFunction, cmap=matplotlib.cm.RdBu_r)

but this gives the error:

  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/axes.py", line 4054, in contourf
    return ContourSet(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/matplotlib/contour.py", line 456, in __init__
    for level, level_upper, color in zip(lowers, uppers, self.tcolors):
AttributeError: ContourSet instance has no attribute 'tcolors'

Any suggestions?

Thanks,
Mike

------------------------------------------------------------------------

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

That worked perfectly. Thanks!

-Mike

···

On Nov 29, 2006, at 11:56 AM, Eric Firing wrote:

linspace(minVal, maxVal, 20)