18
18
from transforms import blended_transform_factory
19
19
from matplotlib import MatplotlibDeprecationWarning as mplDeprecation
20
20
21
+
21
22
class LockDraw :
22
23
"""
23
24
Some widgets, like the cursor, draw onto the canvas, and this is not
@@ -829,7 +830,8 @@ class Cursor(AxesWidget):
829
830
830
831
and the visibility of the cursor itself with the *visible* attribute
831
832
"""
832
- def __init__ (self , ax , horizOn = True , vertOn = True , useblit = False , ** lineprops ):
833
+ def __init__ (self , ax , horizOn = True , vertOn = True , useblit = False ,
834
+ ** lineprops ):
833
835
"""
834
836
Add a cursor to *ax*. If ``useblit=True``, use the backend-
835
837
dependent blitting features for faster updates (GTKAgg
@@ -907,7 +909,8 @@ def _update(self):
907
909
908
910
class MultiCursor (Widget ):
909
911
"""
910
- Provide a vertical line cursor shared between multiple axes
912
+ Provide a vertical (default) and/or horizontal line cursor shared between
913
+ multiple axes
911
914
912
915
Example usage::
913
916
@@ -925,16 +928,23 @@ class MultiCursor(Widget):
925
928
ax2 = fig.add_subplot(212, sharex=ax1)
926
929
ax2.plot(t, s2)
927
930
928
- multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1)
931
+ multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1,
932
+ horizOn=False, vertOn=True)
929
933
show()
930
934
931
935
"""
932
- def __init__ (self , canvas , axes , useblit = True , ** lineprops ):
936
+ def __init__ (self , canvas , axes , useblit = True , horizOn = False , vertOn = True ,
937
+ ** lineprops ):
933
938
934
939
self .canvas = canvas
935
940
self .axes = axes
941
+ self .horizOn = horizOn
942
+ self .vertOn = vertOn
943
+
936
944
xmin , xmax = axes [- 1 ].get_xlim ()
945
+ ymin , ymax = axes [- 1 ].get_ylim ()
937
946
xmid = 0.5 * (xmin + xmax )
947
+ ymid = 0.5 * (ymin + ymax )
938
948
939
949
self .visible = True
940
950
self .useblit = useblit and self .canvas .supports_blit
@@ -943,18 +953,28 @@ def __init__(self, canvas, axes, useblit=True, **lineprops):
943
953
944
954
if useblit :
945
955
lineprops ['animated' ] = True
946
- self .lines = [ax .axvline (xmid , visible = False , ** lineprops )
947
- for ax in axes ]
956
+
957
+ if vertOn :
958
+ self .vlines = [ax .axvline (xmid , visible = False , ** lineprops )
959
+ for ax in axes ]
960
+ else :
961
+ self .vlines = []
962
+
963
+ if horizOn :
964
+ self .hlines = [ax .axhline (ymid , visible = False , ** lineprops )
965
+ for ax in axes ]
966
+ else :
967
+ self .hlines = []
948
968
949
969
self .canvas .mpl_connect ('motion_notify_event' , self .onmove )
950
970
self .canvas .mpl_connect ('draw_event' , self .clear )
951
971
952
972
def clear (self , event ):
953
973
"""clear the cursor"""
954
974
if self .useblit :
955
- self .background = self . canvas . copy_from_bbox (
956
- self .canvas .figure .bbox )
957
- for line in self .lines :
975
+ self .background = (
976
+ self .canvas .copy_from_bbox ( self . canvas . figure .bbox ) )
977
+ for line in self .vlines + self . hlines :
958
978
line .set_visible (False )
959
979
960
980
def onmove (self , event ):
@@ -965,19 +985,26 @@ def onmove(self, event):
965
985
self .needclear = True
966
986
if not self .visible :
967
987
return
968
-
969
- for line in self .lines :
970
- line .set_xdata ((event .xdata , event .xdata ))
971
- line .set_visible (self .visible )
988
+ if self .vertOn :
989
+ for line in self .vlines :
990
+ line .set_xdata ((event .xdata , event .xdata ))
991
+ line .set_visible (self .visible )
992
+ if self .horizOn :
993
+ for line in self .hlines :
994
+ line .set_ydata ((event .ydata , event .ydata ))
995
+ line .set_visible (self .visible )
972
996
self ._update ()
973
997
974
998
def _update (self ):
975
-
976
999
if self .useblit :
977
1000
if self .background is not None :
978
1001
self .canvas .restore_region (self .background )
979
- for ax , line in zip (self .axes , self .lines ):
980
- ax .draw_artist (line )
1002
+ if self .vertOn :
1003
+ for ax , line in zip (self .axes , self .vlines ):
1004
+ ax .draw_artist (line )
1005
+ if self .horizOn :
1006
+ for ax , line in zip (self .axes , self .hlines ):
1007
+ ax .draw_artist (line )
981
1008
self .canvas .blit (self .canvas .figure .bbox )
982
1009
else :
983
1010
@@ -1349,7 +1376,7 @@ def ignore(self, event):
1349
1376
# boundaries.
1350
1377
if event .button == self .eventpress .button and event .inaxes != self .ax :
1351
1378
(xdata , ydata ) = self .ax .transData .inverted ().transform_point (
1352
- (event .x , event .y ))
1379
+ (event .x , event .y ))
1353
1380
x0 , x1 = self .ax .get_xbound ()
1354
1381
y0 , y1 = self .ax .get_ybound ()
1355
1382
xdata = max (x0 , xdata )
@@ -1407,7 +1434,7 @@ def release(self, event):
1407
1434
yproblems = self .minspany is not None and spany < self .minspany
1408
1435
1409
1436
if (((self .drawtype == 'box' ) or (self .drawtype == 'line' )) and
1410
- (xproblems or yproblems )):
1437
+ (xproblems or yproblems )):
1411
1438
# check if drawn distance (if it exists) is not too small in
1412
1439
# neither x nor y-direction
1413
1440
return
0 commit comments