Exemple #1
0
class Plotter(wx.Panel):
    def __init__(self, parent, id = -1, dpi = None, data = None, **kwargs):
        # TODO: inherit directly from Canvas --- it is after all a panel.
        self.data = data

        # Set up the panel parameters
        #style = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__(self, parent, id = id, **kwargs)
        self.figure = mpl.figure.Figure(dpi=dpi,figsize=(1,1))
        self.canvas = Canvas(self, -1, self.figure)
        self.toolbar = Toolbar(self.canvas)
        self.toolbar.set_status_bar(parent.GetStatusBar())
        self.toolbar.Realize()
        self.canvas.mpl_connect('resize_event',self.onResize)
        self.canvas.mpl_connect('scroll_event',self.onWheel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas,1,wx.EXPAND)
        sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.SetSizer(sizer)

    def onResize(self,event):
        self.data.plot(self.figure)

    def onWheel(self, event):
        """
        Process mouse wheel as zoom events
        """
        ax = event.inaxes
        step = event.step

        # Icky hardcoding of colorbar zoom.
        if False and ax == self.coloraxes:
            # rescale colormap: the axes are already scaled to 0..1,
            # so use bal instead of pt for centering
            lo,hi = self.colormapper.get_clim()
            lo,hi = _rescale(lo,hi,step,bal=event.ydata,scale=self.vscale)
            self.colormapper.set_clim(lo,hi)
        elif ax != None:
            # Event occurred inside a plotting area
            lo,hi = ax.get_xlim()
            lo,hi = _rescale(lo,hi,step,pt=event.xdata)
            ax.set_xlim((lo,hi))

            lo,hi = ax.get_ylim()
            lo,hi = _rescale(lo,hi,step,pt=event.ydata)
            ax.set_ylim((lo,hi))
        else:
            # Check if zoom happens in the axes
            xdata,ydata = None,None
            x,y = event.x,event.y
            for ax in self.figure.axes:
                insidex,_ = ax.xaxis.contains(event)
                if insidex:
                    xdata,_ = _invert(ax,x,y)
                    #print "xaxis",x,"->",xdata
                insidey,_ = ax.yaxis.contains(event)
                if insidey:
                    _,ydata = _invert(ax,x,y)
                    #print "yaxis",y,"->",ydata
            if xdata is not None:
                lo,hi = ax.get_xlim()
                lo,hi = _rescale(lo,hi,step,bal=xdata)
                ax.set_xlim((lo,hi))
            if ydata is not None:
                lo,hi = ax.get_ylim()
                lo,hi = _rescale(lo,hi,step,bal=ydata)
                ax.set_ylim((lo,hi))

        self.canvas.draw()
Exemple #2
0
class Plotter(wx.Panel):
    def __init__(self, parent, id=-1, dpi=None, data=None, **kwargs):
        # TODO: inherit directly from Canvas --- it is after all a panel.
        self.data = data

        # Set up the panel parameters
        #style = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__(self, parent, id=id, **kwargs)
        self.figure = mpl.figure.Figure(dpi=dpi, figsize=(1, 1))
        self.canvas = Canvas(self, -1, self.figure)
        self.toolbar = Toolbar(self.canvas)
        self.toolbar.set_status_bar(parent.GetStatusBar())
        self.toolbar.Realize()
        self.canvas.mpl_connect('resize_event', self.onResize)
        self.canvas.mpl_connect('scroll_event', self.onWheel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.EXPAND)
        sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.SetSizer(sizer)

    def onResize(self, event):
        self.data.plot(self.figure)

    def onWheel(self, event):
        """
        Process mouse wheel as zoom events
        """
        ax = event.inaxes
        step = event.step

        # Icky hardcoding of colorbar zoom.
        if False and ax == self.coloraxes:
            # rescale colormap: the axes are already scaled to 0..1,
            # so use bal instead of pt for centering
            lo, hi = self.colormapper.get_clim()
            lo, hi = _rescale(lo, hi, step, bal=event.ydata, scale=self.vscale)
            self.colormapper.set_clim(lo, hi)
        elif ax != None:
            # Event occurred inside a plotting area
            lo, hi = ax.get_xlim()
            lo, hi = _rescale(lo, hi, step, pt=event.xdata)
            ax.set_xlim((lo, hi))

            lo, hi = ax.get_ylim()
            lo, hi = _rescale(lo, hi, step, pt=event.ydata)
            ax.set_ylim((lo, hi))
        else:
            # Check if zoom happens in the axes
            xdata, ydata = None, None
            x, y = event.x, event.y
            for ax in self.figure.axes:
                insidex, _ = ax.xaxis.contains(event)
                if insidex:
                    xdata, _ = _invert(ax, x, y)
                    #print "xaxis",x,"->",xdata
                insidey, _ = ax.yaxis.contains(event)
                if insidey:
                    _, ydata = _invert(ax, x, y)
                    #print "yaxis",y,"->",ydata
            if xdata is not None:
                lo, hi = ax.get_xlim()
                lo, hi = _rescale(lo, hi, step, bal=xdata)
                ax.set_xlim((lo, hi))
            if ydata is not None:
                lo, hi = ax.get_ylim()
                lo, hi = _rescale(lo, hi, step, bal=ydata)
                ax.set_ylim((lo, hi))

        self.canvas.draw()