Skip to content

Commit f17c5b1

Browse files
committed
added multicursor
svn path=/trunk/matplotlib/; revision=2504
1 parent d074f64 commit f17c5b1

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

examples/widgets/multicursor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from matplotlib.widgets import MultiCursor
2+
from pylab import figure, show, nx
3+
4+
t = nx.arange(0.0, 2.0, 0.01)
5+
s1 = nx.sin(2*nx.pi*t)
6+
s2 = nx.sin(4*nx.pi*t)
7+
fig = figure()
8+
ax1 = fig.add_subplot(211)
9+
ax1.plot(t, s1)
10+
11+
12+
ax2 = fig.add_subplot(212, sharex=ax1)
13+
ax2.plot(t, s2)
14+
15+
multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1)
16+
show()

lib/matplotlib/widgets.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,71 @@ def _update(self):
675675

676676
return False
677677

678+
class MultiCursor:
679+
"""
680+
Provide a vertical line cursor shared between multiple axes
681+
682+
from matplotlib.widgets import MultiCursor
683+
from pylab import figure, show, nx
684+
685+
t = nx.arange(0.0, 2.0, 0.01)
686+
s1 = nx.sin(2*nx.pi*t)
687+
s2 = nx.sin(4*nx.pi*t)
688+
fig = figure()
689+
ax1 = fig.add_subplot(211)
690+
ax1.plot(t, s1)
691+
692+
693+
ax2 = fig.add_subplot(212, sharex=ax1)
694+
ax2.plot(t, s2)
695+
696+
multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1)
697+
show()
698+
699+
"""
700+
def __init__(self, canvas, axes, useblit=True, **lineprops):
701+
self.canvas = canvas
702+
self.canvas.mpl_connect('motion_notify_event', self.onmove)
703+
self.canvas.mpl_connect('draw_event', self.clear)
704+
self.axes = axes
705+
self.lines = [ax.axvline(1, visible=False, **lineprops) for ax in axes]
706+
707+
self.visible = True
708+
self.useblit = useblit
709+
self.background = None
710+
self.needclear = False
711+
712+
713+
def clear(self, event):
714+
'clear the cursor'
715+
if self.useblit:
716+
self.background = self.canvas.copy_from_bbox(self.canvas.figure.bbox)
717+
for line in self.lines: line.set_visible(False)
718+
719+
720+
def onmove(self, event):
721+
722+
if event.inaxes is None: return
723+
self.needclear = True
724+
if not self.visible: return
725+
726+
for line in self.lines:
727+
line.set_xdata((event.xdata, event.xdata))
728+
line.set_visible(self.visible)
729+
self._update()
730+
731+
732+
def _update(self):
733+
if self.useblit:
734+
if self.background is not None:
735+
self.canvas.restore_region(self.background)
736+
for ax, line in zip(self.axes, self.lines):
737+
ax.draw_artist(line)
738+
self.canvas.blit(self.canvas.figure.bbox)
739+
else:
740+
741+
self.canvas.draw_idle()
742+
678743
class SpanSelector:
679744
"""
680745
Select a min/max range of the x or y axes for a matplotlib Axes

0 commit comments

Comments
 (0)