@@ -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
4256class 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
0 commit comments