Skip to content

Commit 8cdd9ea

Browse files
committed
Facilitate display of colormaps with ColorbarBase
svn path=/trunk/matplotlib/; revision=3410
1 parent 6b95a6a commit 8cdd9ea

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

lib/matplotlib/colorbar.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@
110110

111111
class ColorbarBase(cm.ScalarMappable):
112112
'''
113+
Draw a colorbar in an existing axes.
114+
115+
This is a base class for the Colorbar class, which is
116+
the basis for the colorbar method and pylab function.
117+
118+
It is also useful by itself for showing a colormap. If
119+
the cmap kwarg is given but boundaries and values are left
120+
as None, then the colormap will be displayed on a 0-1 scale.
121+
To show the under- and over-value colors, specify the norm
122+
as colors.Normalize(clip=False).
123+
To show the colors versus index instead of on the 0-1 scale,
124+
use norm=colors.NoNorm.
113125
'''
114126
_slice_dict = {'neither': slice(0,1000000),
115127
'both': slice(1,-1),
@@ -348,10 +360,28 @@ def _process_values(self, b=None):
348360
return
349361
self._boundaries = npy.array(self.boundaries)
350362
return
363+
# Neither boundaries nor values are specified;
364+
# make reasonable ones based on cmap and norm.
351365
if isinstance(self.norm, colors.NoNorm):
352-
b = npy.arange(self.norm.vmin, self.norm.vmax + 2) - 0.5
366+
b = self._uniform_y(self.cmap.N+1) * self.cmap.N - 0.5
367+
v = npy.zeros((len(b)-1,), dtype=npy.int16)
368+
v[self._inside] = npy.arange(self.cmap.N, dtype=npy.int16)
369+
if self.extend in ('both', 'min'):
370+
v[0] = -1
371+
if self.extend in ('both', 'max'):
372+
v[-1] = self.cmap.N
373+
self._boundaries = b
374+
self._values = v
375+
return
353376
else:
377+
if not self.norm.scaled():
378+
self.norm.vmin = 0
379+
self.norm.vmax = 1
354380
b = self.norm.inverse(self._uniform_y(self.cmap.N+1))
381+
if self.extend in ('both', 'min'):
382+
b[0] = b[0] - 1
383+
if self.extend in ('both', 'max'):
384+
b[-1] = b[-1] + 1
355385
self._process_values(b)
356386

357387
def _find_range(self):

0 commit comments

Comments
 (0)