Skip to content

Commit 0e580c9

Browse files
committed
added colormap support to labels
svn path=/trunk/matplotlib/; revision=1063
1 parent 1ee2dba commit 0e580c9

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

MANIFEST

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ examples/color_demo.py
267267
examples/colours.py
268268
examples/contour_demo.py
269269
examples/contour_demo2.py
270+
examples/contour_image.py
270271
examples/contourf_demo.py
271272
examples/coords_demo.py
272273
examples/coords_report.py
@@ -528,6 +529,8 @@ lib/matplotlib/cm.py
528529
lib/matplotlib/collections.py
529530
lib/matplotlib/collections.py.~1.15.~
530531
lib/matplotlib/colors.py
532+
lib/matplotlib/contour.py
533+
lib/matplotlib/contour.py~
531534
lib/matplotlib/dates.py
532535
lib/matplotlib/figure.py
533536
lib/matplotlib/figure.py.~1.16.~

boilerplate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def %(func)s(*args, **kwargs):
8585
)
8686

8787
cmappable = {
88+
'clabel' : 'if ret.mappable is not None: gci._current = ret.mappable',
8889
'contour' : 'if ret[1].mappable is not None: gci._current = ret[1].mappable',
8990
'contourf': 'if ret[1].mappable is not None: gci._current = ret[1].mappable',
9091
'scatter' : 'gci._current = ret',

examples/backend_driver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
'bar_stacked.py',
1414
'barchart_demo.py',
1515
'color_demo.py',
16+
'contour_demo.py',
17+
'contour_demo2.py',
18+
'contourf_demo.py',
1619
'csd_demo.py',
1720
'custom_ticker1.py',
1821
'customize_rc.py',

examples/contourf_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
origin = 'lower'
55
#origin = 'upper'
66

7-
delta = 0.5 #0.025
7+
delta = 0.025
88
x = y = arange(-3.0, 3.01, delta)
99
X, Y = meshgrid(x, y)
1010
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

lib/matplotlib/cm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ def set_array(self, A):
421421
'Set the image array from numeric/numarray A'
422422
self._A = A.astype(nx.Float32)
423423

424+
def get_array(self):
425+
'Return the array'
426+
return self._A
427+
424428
def set_clim(self, vmin=None, vmax=None):
425429
'set the norm limits for image scaling'
426430
self.norm.vmin = vmin

lib/matplotlib/contour.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,29 @@ class ContourMappable(ScalarMappable):
2828
"""
2929
a class to allow contours to respond properly to change in cmaps, etc
3030
"""
31-
def __init__(self, levels, collections, norm=None, cmap=None):
31+
def __init__(self, levels, collections, norm=None, cmap=None, labeld=None):
32+
"""
33+
See comment on labeld in the ContourLabeler class
34+
35+
"""
3236
ScalarMappable.__init__(self, norm, cmap)
3337
self.levels = levels
3438
self.collections = collections
39+
if labeld is None: labeld = {}
40+
self.labeld = labeld
3541

3642
def changed(self):
3743
colors = [ (tuple(rgba),) for rgba in self.to_rgba(self.levels)]
44+
contourNum = 0
3845
for color, collection in zip(colors, self.collections):
3946
collection.set_color(color)
47+
Ncolor = len(color) # collections could have more than 1 in principle
48+
for segNum, segment in enumerate(collection._segments):
49+
key = contourNum, segNum
50+
t = self.labeld.get(key)
51+
if t is not None: t.set_color(color[segNum%Ncolor])
52+
contourNum += 1
53+
4054
ScalarMappable.changed(self)
4155

4256
class ContourLabeler:
@@ -128,6 +142,13 @@ def clabel(self, *args, **kwargs):
128142
self.cl = []
129143
self.cl_xy = []
130144

145+
# we have a list of contours and each contour has a list of
146+
# segments. We want changes in the contour color to be
147+
# reflected in changes in the label color. This is a good use
148+
# for traits observers, but in the interim, until traits are
149+
# utilized, we'll create a dict mapping i,j to text instances.
150+
# i is the contour level index, j is the sement index
151+
self.labeld = {}
131152
if inline == 1:
132153
toremove, toadd = self.inline_labels(levels, contours, colors, fslist, fmt)
133154
for r in toremove:
@@ -139,7 +160,19 @@ def clabel(self, *args, **kwargs):
139160

140161
for label in self.cl:
141162
self.ax.add_artist(label)
142-
return silent_list('Text', self.cl)
163+
164+
165+
if hasattr(contours, 'mappable'):
166+
old = getattr(contours, 'mappable')
167+
mappable = ContourMappable(old.get_array(), toadd, cmap=old.cmap, labeld=self.labeld)
168+
mappable.set_array(old.get_array())
169+
mappable.autoscale()
170+
else:
171+
mappable = None
172+
173+
ret = silent_list('Text', self.cl)
174+
ret.mappable = mappable
175+
return ret
143176

144177

145178

@@ -316,10 +349,12 @@ def inline_labels(self, levels, contours, colors, fslist, fmt):
316349
toremove = []
317350
toadd = []
318351
trans = self.ax.transData
352+
contourNum = 0
319353
for lev, con, color, fsize in zip(levels, contours, colors, fslist):
320354
col = []
321355
lw = self.get_label_width(lev, fmt, fsize)
322-
for linecontour in con._segments:
356+
for segNum, linecontour in enumerate(con._segments):
357+
key = contourNum, segNum
323358
# for closed contours add one more point to
324359
# avoid division by zero
325360
if linecontour[0] == linecontour[-1]:
@@ -332,15 +367,18 @@ def inline_labels(self, levels, contours, colors, fslist, fmt):
332367
# data coordinates
333368
dx,dy = trans.inverse_xy_tup((x,y))
334369
t = Text(dx, dy, rotation = rotation, horizontalalignment='center', verticalalignment='center')
370+
self.labeld[key] = t
335371
text = self.get_text(lev,fmt)
336372
self.set_label_props(t, text, color)
337373
self.cl.append(t)
338374

339375
new = self.break_linecontour(linecontour, rotation, lw, ind)
340376
for c in new: col.append(c)
341-
else: col.append(linecontour)
377+
else:
378+
col.append(linecontour)
342379
toremove.append(con)
343380
toadd.append(LineCollection(col, colors=con._colors, linewidths = con._lw))
381+
contourNum += 1
344382

345383
return toremove, toadd
346384

lib/matplotlib/pylab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ def clabel(*args, **kwargs):
17971797
except:
17981798
hold(b)
17991799
raise
1800-
1800+
if ret.mappable is not None: gci._current = ret.mappable
18011801
hold(b)
18021802
return ret
18031803
clabel.__doc__ = _shift_string(Axes.clabel.__doc__) + """

0 commit comments

Comments
 (0)