I just spent some time debugging an odd problem. Take this code:
x,y = np.arange(-10,10), np.arange(-10,10)
x,y = np.meshgrid(x,y)
z = x**2+y**2
cs = pyplot.contourf(x,y,z,levels=np.arange(50, 220, 20),
cmap=pyplot.cm.jet, extend='both')
cs.cmap.set_under('k')
cb = pyplot.colorbar(cs)
I was expecting the set_under call to mean that contours outside the
passed level range would be painted in that color. This seems to be
what the documentation says will happen:
"Unless this [extend argument] is ‘neither’, contour levels are
automatically added to one or both ends of the range so that all data
are included. These added ranges are then mapped to the special
colormap values which default to the ends of the colormap range, but
can be set via matplotlib.colors.Colormap.set_under() and
matplotlib.colors.Colormap.set_over() methods."
. . .but it doesn't work. Instead, the levels outside my specified
range show up colored the same as the lowest selected level (i.e., 50).
I poked around in the code and found that the culprit is this section
in matplotlib.contour.ContourSet._process_levels():
if self.extend in ('both', 'min'):
self.vmin = 2 * self.levels[0] - self.levels[1]
if self.extend in ('both', 'max'):
self.vmax = 2 * self.levels[-1] - self.levels[-2]
Here, if the "extend" argument is given, the code sets the data limits
to some odd extension of the actual data limits. I can fix it and get
the correct output by resetting the data limits after plotting my
contours:
cs = pyplot.contourf(x,y,z,levels=np.arange(50, 220, 20),
cmap=pyplot.cm.jet, extend='both')
cs.cmap.set_under('k')
cs.set_clim(50, 210)
cb = pyplot.colorbar(cs)
But why do I have to do this? The whole reason I passed in my
specified levels was because I wanted THOSE to be the data limits.
Why is matplotlib expanding the data limits, and thus preventing me
from specifying the "out of range" color using the normal set_under
and set_over methods?
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users