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
@@ -926,15 +929,23 @@ class MultiCursor(Widget):
926
929
ax2.plot(t, s2)
927
930
928
931
multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1)
932
+ #multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1,
933
+ # horizOn=True, vertOn=True)
929
934
show()
930
935
931
936
"""
932
- def __init__ (self , canvas , axes , useblit = True , ** lineprops ):
937
+ def __init__ (self , canvas , axes , useblit = True , horizOn = False , vertOn = True ,
938
+ ** lineprops ):
933
939
934
940
self .canvas = canvas
935
941
self .axes = axes
942
+ self .horizOn = horizOn
943
+ self .vertOn = vertOn
944
+
936
945
xmin , xmax = axes [- 1 ].get_xlim ()
946
+ ymin , ymax = axes [- 1 ].get_ylim ()
937
947
xmid = 0.5 * (xmin + xmax )
948
+ ymid = 0.5 * (ymin + ymax )
938
949
939
950
self .visible = True
940
951
self .useblit = useblit and self .canvas .supports_blit
@@ -943,18 +954,28 @@ def __init__(self, canvas, axes, useblit=True, **lineprops):
943
954
944
955
if useblit :
945
956
lineprops ['animated' ] = True
946
- self .lines = [ax .axvline (xmid , visible = False , ** lineprops )
947
- for ax in axes ]
957
+
958
+ if vertOn :
959
+ self .vlines = [ax .axvline (xmid , visible = False , ** lineprops )
960
+ for ax in axes ]
961
+ else :
962
+ self .vlines = []
963
+
964
+ if horizOn :
965
+ self .hlines = [ax .axhline (ymid , visible = False , ** lineprops )
966
+ for ax in axes ]
967
+ else :
968
+ self .hlines = []
948
969
949
970
self .canvas .mpl_connect ('motion_notify_event' , self .onmove )
950
971
self .canvas .mpl_connect ('draw_event' , self .clear )
951
972
952
973
def clear (self , event ):
953
974
"""clear the cursor"""
954
975
if self .useblit :
955
- self .background = self . canvas . copy_from_bbox (
956
- self .canvas .figure .bbox )
957
- for line in self .lines :
976
+ self .background = (
977
+ self .canvas .copy_from_bbox ( self . canvas . figure .bbox ) )
978
+ for line in self .vlines + self . hlines :
958
979
line .set_visible (False )
959
980
960
981
def onmove (self , event ):
@@ -965,19 +986,26 @@ def onmove(self, event):
965
986
self .needclear = True
966
987
if not self .visible :
967
988
return
968
-
969
- for line in self .lines :
970
- line .set_xdata ((event .xdata , event .xdata ))
971
- line .set_visible (self .visible )
989
+ if self .vertOn :
990
+ for line in self .vlines :
991
+ line .set_xdata ((event .xdata , event .xdata ))
992
+ line .set_visible (self .visible )
993
+ if self .horizOn :
994
+ for line in self .hlines :
995
+ line .set_ydata ((event .ydata , event .ydata ))
996
+ line .set_visible (self .visible )
972
997
self ._update ()
973
998
974
999
def _update (self ):
975
-
976
1000
if self .useblit :
977
1001
if self .background is not None :
978
1002
self .canvas .restore_region (self .background )
979
- for ax , line in zip (self .axes , self .lines ):
980
- ax .draw_artist (line )
1003
+ if self .vertOn :
1004
+ for ax , line in zip (self .axes , self .vlines ):
1005
+ ax .draw_artist (line )
1006
+ if self .horizOn :
1007
+ for ax , line in zip (self .axes , self .hlines ):
1008
+ ax .draw_artist (line )
981
1009
self .canvas .blit (self .canvas .figure .bbox )
982
1010
else :
983
1011
@@ -1349,7 +1377,7 @@ def ignore(self, event):
1349
1377
# boundaries.
1350
1378
if event .button == self .eventpress .button and event .inaxes != self .ax :
1351
1379
(xdata , ydata ) = self .ax .transData .inverted ().transform_point (
1352
- (event .x , event .y ))
1380
+ (event .x , event .y ))
1353
1381
x0 , x1 = self .ax .get_xbound ()
1354
1382
y0 , y1 = self .ax .get_ybound ()
1355
1383
xdata = max (x0 , xdata )
@@ -1407,7 +1435,7 @@ def release(self, event):
1407
1435
yproblems = self .minspany is not None and spany < self .minspany
1408
1436
1409
1437
if (((self .drawtype == 'box' ) or (self .drawtype == 'line' )) and
1410
- (xproblems or yproblems )):
1438
+ (xproblems or yproblems )):
1411
1439
# check if drawn distance (if it exists) is not too small in
1412
1440
# neither x nor y-direction
1413
1441
return
0 commit comments