220
220
221
221
class QuiverKey (martist .Artist ):
222
222
""" Labelled arrow for use as a quiver plot scale key."""
223
- halign = {'N' : 'center' , 'S' : 'center' , 'E' : 'left' , 'W' : 'right' }
224
- valign = {'N' : 'bottom' , 'S' : 'top' , 'E' : 'center' , 'W' : 'center' }
225
- pivot = {'N' : 'mid' , 'S' : 'mid' , 'E' : 'tip' , 'W' : 'tail' }
223
+ halign = {'N' : 'center' , 'S' : 'center' , 'E' : 'left' , 'W' : 'right' }
224
+ valign = {'N' : 'bottom' , 'S' : 'top' , 'E' : 'center' , 'W' : 'center' }
225
+ pivot = {'N' : 'mid' , 'S' : 'mid' , 'E' : 'tip' , 'W' : 'tail' }
226
226
227
227
def __init__ (self , Q , X , Y , U , label , ** kw ):
228
228
martist .Artist .__init__ (self )
@@ -236,13 +236,21 @@ def __init__(self, Q, X, Y, U, label, **kw):
236
236
self ._labelsep_inches = kw .pop ('labelsep' , 0.1 )
237
237
self .labelsep = (self ._labelsep_inches * Q .ax .figure .dpi )
238
238
239
+ # try to prevent closure over the real self
240
+ weak_self = weakref .ref (self )
241
+
239
242
def on_dpi_change (fig ):
240
- self .labelsep = (self ._labelsep_inches * fig .dpi )
241
- self ._initialized = False # simple brute force update
242
- # works because _init is called
243
- # at the start of draw.
243
+ _s = weak_self ()
244
+ if _s is not None :
245
+ _s .labelsep = (_s ._labelsep_inches * fig .dpi )
246
+ _s ._initialized = False # simple brute force update
247
+ # works because _init is called
248
+ # at the start of draw.
249
+
250
+ self ._cid = Q .ax .figure .callbacks .connect ('dpi_changed' ,
251
+ on_dpi_change )
244
252
245
- Q .ax .figure .callbacks . connect ( 'dpi_changed' , on_dpi_change )
253
+ self . _cb_ref = weakref . ref ( Q .ax .figure .callbacks )
246
254
247
255
self .labelpos = kw .pop ('labelpos' , 'N' )
248
256
self .labelcolor = kw .pop ('labelcolor' , None )
@@ -255,11 +263,24 @@ def on_dpi_change(fig):
255
263
horizontalalignment = self .halign [self .labelpos ],
256
264
verticalalignment = self .valign [self .labelpos ],
257
265
fontproperties = font_manager .FontProperties (** _fp ))
266
+
258
267
if self .labelcolor is not None :
259
268
self .text .set_color (self .labelcolor )
260
269
self ._initialized = False
261
270
self .zorder = Q .zorder + 0.1
262
271
272
+ def remove (self ):
273
+ """
274
+ Overload the remove method
275
+ """
276
+ _cbs = self ._cb_ref ()
277
+ if _cbs is not None :
278
+ # disconnect the call back
279
+ _cbs .disconnect (self ._cid )
280
+ self ._cid = None
281
+ # pass the remove call up the stack
282
+ martist .Artist .remove (self )
283
+
263
284
__init__ .__doc__ = _quiverkey_doc
264
285
265
286
def _init (self ):
0 commit comments