10000 MultiCursor with optionnal horizontal bar · matplotlib/matplotlib@349ccec · GitHub
[go: up one dir, main page]

Skip to content

Commit 349ccec

Browse files
committed
MultiCursor with optionnal horizontal bar
1 parent e45667e commit 349ccec

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

lib/matplotlib/widgets.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from transforms import blended_transform_factory
1919
from matplotlib import MatplotlibDeprecationWarning as mplDeprecation
2020

21+
2122
class LockDraw:
2223
"""
2324
Some widgets, like the cursor, draw onto the canvas, and this is not
@@ -829,7 +830,8 @@ class Cursor(AxesWidget):
829830
830831
and the visibility of the cursor itself with the *visible* attribute
831832
"""
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):
833835
"""
834836
Add a cursor to *ax*. If ``useblit=True``, use the backend-
835837
dependent blitting features for faster updates (GTKAgg
@@ -907,7 +909,8 @@ def _update(self):
907909

908910
class MultiCursor(Widget):
909911
"""
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
911914
912915
Example usage::
913916
@@ -926,15 +929,23 @@ class MultiCursor(Widget):
926929
ax2.plot(t, s2)
927930
928931
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)
929934
show()
930935
931936
"""
932-
def __init__(self, canvas, axes, useblit=True, **lineprops):
937+
def __init__(self, canvas, axes, useblit=True, horizOn=False, vertOn=True,
938+
**lineprops):
933939

934940
self.canvas = canvas
935941
self.axes = axes
942+
self.horizOn = horizOn
943+
self.vertOn = vertOn
944+
936945
xmin, xmax = axes[-1].get_xlim()
946+
ymin, ymax = axes[-1].get_ylim()
937947
xmid = 0.5 * (xmin + xmax)
948+
ymid = 0.5 * (ymin + ymax)
938949

939950
self.visible = True
940951
self.useblit = useblit and self.canvas.supports_blit
@@ -943,18 +954,28 @@ def __init__(self, canvas, axes, useblit=True, **lineprops):
943954

944955
if useblit:
945956
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 = []
948969

949970
self.canvas.mpl_connect('motion_notify_event', self.onmove)
950971
self.canvas.mpl_connect('draw_event', self.clear)
951972

952973
def clear(self, event):
953974
"""clear the cursor"""
954975
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:
958979
line.set_visible(False)
959980

960981
def onmove(self, event):
@@ -965,19 +986,26 @@ def onmove(self, event):
965986
self.needclear = True
966987
if not self.visible:
967988
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)
972997
self._update()
973998

974999
def _update(self):
975-
9761000
if self.useblit:
9771001
if self.background is not None:
9781002
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)
9811009
self.canvas.blit(self.canvas.figure.bbox)
9821010
else:
9831011

@@ -1349,7 +1377,7 @@ def ignore(self, event):
13491377
# boundaries.
13501378
if event.button == self.eventpress.button and event.inaxes != self.ax:
13511379
(xdata, ydata) = self.ax.transData.inverted().transform_point(
1352-
(event.x, event.y))
1380+
(event.x, event.y))
13531381
x0, x1 = self.ax.get_xbound()
13541382
y0, y1 = self.ax.get_ybound()
13551383
xdata = max(x0, xdata)
@@ -1407,7 +1435,7 @@ def release(self, event):
14071435
yproblems = self.minspany is not None and spany < self.minspany
14081436

14091437
if (((self.drawtype == 'box') or (self.drawtype == 'line')) and
1410-
(xproblems or yproblems)):
1438+
(xproblems or yproblems)):
14111439
# check if drawn distance (if it exists) is not too small in
14121440
# neither x nor y-direction
14131441
return

0 commit comments

Comments
 (0)
0