00001 # Software License Agreement (BSD License) 00002 # 00003 # Copyright (c) 2011, UC Regents 00004 # All rights reserved. 00005 # 00006 # Redistribution and use in source and binary forms, with or without 00007 # modification, are permitted provided that the following conditions 00008 # are met: 00009 # 00010 # * Redistributions of source code must retain the above copyright 00011 # notice, this list of conditions and the following disclaimer. 00012 # * Redistributions in binary form must reproduce the above 00013 # copyright notice, this list of conditions and the following 00014 # disclaimer in the documentation and/or other materials provided 00015 # with the distribution. 00016 # * Neither the name of the University of California nor the names of its 00017 # contributors may be used to endorse or promote products derived 00018 # from this software without specific prior written permission. 00019 # 00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 # POSSIBILITY OF SUCH DAMAGE. 00032 00033 from matplotlib import pyplot as plt 00034 00035 class TimeSeriesCursorSynchronizer(object): 00036 00037 def __init__(self): 00038 self.axes = [] 00039 self.cursors = {} 00040 self.axvlines = {} 00041 00042 def add_axis(self, ax): 00043 self.axes.append(ax) 00044 self.axvlines[ax] = [] 00045 self._create_cursor(ax) 00046 00047 def _create_cursor(self, ax): 00048 cur = plt.axvline(0, color='r') 00049 self.cursors[ax] = cur 00050 00051 def _redraw_axis(self, ax): 00052 ax.get_figure().canvas.draw() # maybe not most efficient way..? 00053 00054 def set_cursors(self, t): 00055 for ax in self.axes: 00056 cur = self.cursors[ax] 00057 cur.set_xdata([t, t]) 00058 self._redraw_axis(ax) 00059 00060 def add_axvline(self, t): 00061 for ax in self.axes: 00062 axvline = ax.axvline(t) 00063 self.axvlines[ax].append(axvline) 00064 self._redraw_axis(ax) 00065 00066 def clear_axvlines(self): 00067 for ax, axvlines in self.axvlines.items(): 00068 for axvline in axvlines: 00069 axvline.remove() 00070 self.axvlines[ax] = [] 00071 self._redraw_axis(ax) 00072 00073 def sync_xrange(self, src_ax): 00074 limits = src_ax.get_xlim() 00075 for ax in self.axes: 00076 if ax is not src_ax: 00077 ax.set_xlim(limits) 00078 self._redraw_axis(ax) 00079 00080