Ejemplo n.º 1
0
class PlotPanel(wx.Panel):
    """
    The PlotPanel
    """
    def __init__(self,
                 parent,
                 name=None,
                 color=None,
                 dpi=None,
                 n_axes=1,
                 **kwargs):
        # initialize Panel
        if 'id' not in iter(kwargs.keys()):
            kwargs['id'] = wx.ID_ANY
        if 'style' not in iter(kwargs.keys()):
            kwargs['style'] = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__(self, parent, **kwargs)
        self.SetMinSize((100, 40))

        # The logging logger instance.
        loggerName = __name__ + "." + self.__class__.__name__
        self.logger = logging.getLogger(loggerName)

        # The name of the plot panel.
        self.name = name

        # initialize matplotlib stuff
        self.figure = mpl.figure.Figure(None, dpi=dpi, facecolor='white')
        self.canvas = FigureCanvas(self, -1, self.figure)

        # The axes.
        self._axes = []
        axes_height = 1 / n_axes
        for k in range(n_axes):
            self._axes.append(
                self.figure.add_axes([0, k * axes_height, 1, axes_height]))
        self.canvas.SetMinSize((30, 10))
        self.SetBackgroundColour('white')

        # The axes backgrounds for blit animation.
        self.blit_backgrounds = []

        # Add the canvas to the sizer.
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        self.canvas.Bind(wx.EVT_SET_FOCUS, self.on_set_focus)
        self.Bind(wx.EVT_SET_FOCUS, self.on_set_focus2)
        self.canvas.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
        self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
        self.canvas.Bind(wx.EVT_KEY_UP, self.on_key_up)
        self.Bind(wx.EVT_KEY_UP, self.on_key_up)
        self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
        self.canvas.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)

    @property
    def axes(self):
        ''' The axes of the panel.

        If only one axis is present, return the axes, otherwise return a list of axes.
        '''
        if len(self._axes) == 1:
            return self._axes[0]
        else:
            return self._axes

    def on_wx_xlick(self, event):
        self.logger.debug("on_wx_xlick in plot_panel %s. event: %s", self.name,
                          event)

    def on_set_focus(self, event):
        self.logger.debug("on_set_focus in plot_panel %s. event: %s",
                          self.name, event)
        self.logger.debug("Event should propagate: %s",
                          event.ShouldPropagate())
        #event.ResumePropagation(1)
        event.Skip()

    def on_set_focus2(self, event):
        self.logger.debug("on_set_focus2 in plot_panel %s. event: %s",
                          self.name, event)

    def on_key_down(self, event):
        self.logger.debug("on_key_down in plot_panel %s. event: %s", self.name,
                          event)
        event.ResumePropagation(1)
        event.Skip()

    def on_key_up(self, event):
        self.logger.debug("on_key_up in plot_panel %s. event: %s", self.name,
                          event)
        event.ResumePropagation(1)
        event.Skip()

    def on_left_down(self, event):
        self.logger.debug("on_left_down in plot_panel %s. event: %s",
                          self.name, event)
        event.ResumePropagation(30)
        event.Skip()

    def set_color(self, rgbtuple=None):
        ''' Set figure and canvas colours to be the same.
        '''
        if rgbtuple is None:
            rgbtuple = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get()
        clr = [c / 255. for c in rgbtuple]
        self.figure.set_facecolor(clr)
        self.figure.set_edgecolor(clr)
        self.canvas.SetBackgroundColour(wx.Colour(*rgbtuple))
        self.canvas.Refresh()

    def set_n_axes(self, n_axes):
        ''' Set the number of axes provided by the plot panel.
        '''
        # Delete the existing axes.
        for cur_ax in self._axes:
            self.figure.delaxes(cur_ax)
        self._axes = []

        # Create the new number of axes.
        axes_height = old_div(1, n_axes)
        for k in range(n_axes):
            self._axes.append(
                self.figure.add_axes([0, k * axes_height, 1, axes_height]))

    def draw(self):
        ''' Draw the canvas to make the changes visible.
        '''
        self.canvas.draw()

    def save_blit_background(self):
        ''' Save the axes background for animation.
        '''
        self.blit_backgrounds = []
        for cur_axes in self._axes:
            self.blit_backgrounds.append(
                self.canvas.copy_from_bbox(cur_axes.bbox))

    def restore_blit_background(self):
        ''' Restore the axes background for animation.
        '''
        for k, cur_bg in enumerate(self.blit_backgrounds):
            self.canvas.restore_region(cur_bg, bbox=self._axes[k].bbox)

    def draw_blit_artists(self, artists):
        ''' Draw the artist for animation.
        '''
        for cur_artist in artists:
            for cur_line_artist in cur_artist.line_artist:
                cur_line_artist.axes.draw_artist(cur_line_artist)

            for cur_text_artist in cur_artist.text_artist:
                cur_text_artist.axes.draw_artist(cur_text_artist)

    def update_display(self):
        ''' Update the display of the docking frame.

        '''
        assert False, 'The update_display method must be defined!'
Ejemplo n.º 2
0
class PlotPanel(wx.Panel):
    """
    The PlotPanel
    """
    def __init__(self, parent, name=None, color=None, dpi=None, **kwargs):
        # initialize Panel
        if 'id' not in kwargs.keys():
            kwargs['id'] = wx.ID_ANY
        if 'style' not in kwargs.keys():
            kwargs['style'] = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__(self, parent, **kwargs)
        self.SetMinSize((100, 40))

        # The logging logger instance.
        loggerName = __name__ + "." + self.__class__.__name__
        self.logger = logging.getLogger(loggerName)

        # The name of the plot panel.
        self.name = name

        # initialize matplotlib stuff
        self.figure = mpl.figure.Figure(None, dpi=dpi, facecolor='white')
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.axes = self.figure.add_axes([0, 0, 1, 1])
        self.canvas.SetMinSize((30, 10))
        self.SetBackgroundColour('white')

        # Add the canvas to the sizer.
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        self.canvas.Bind(wx.EVT_SET_FOCUS, self.on_set_focus)
        self.Bind(wx.EVT_SET_FOCUS, self.on_set_focus2)
        self.canvas.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
        self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
        self.canvas.Bind(wx.EVT_KEY_UP, self.on_key_up)
        self.Bind(wx.EVT_KEY_UP, self.on_key_up)
        self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
        self.canvas.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)

    def on_wx_xlick(self, event):
        self.logger.debug("on_wx_xlick in plot_panel %s. event: %s", self.name,
                          event)

    def on_set_focus(self, event):
        self.logger.debug("on_set_focus in plot_panel %s. event: %s",
                          self.name, event)
        self.logger.debug("Event should propagate: %s",
                          event.ShouldPropagate())
        #event.ResumePropagation(1)
        event.Skip()

    def on_set_focus2(self, event):
        self.logger.debug("on_set_focus2 in plot_panel %s. event: %s",
                          self.name, event)

    def on_key_down(self, event):
        self.logger.debug("on_key_down in plot_panel %s. event: %s", self.name,
                          event)
        event.ResumePropagation(1)
        event.Skip()

    def on_key_up(self, event):
        self.logger.debug("on_key_up in plot_panel %s. event: %s", self.name,
                          event)
        event.ResumePropagation(1)
        event.Skip()

    def on_left_down(self, event):
        self.logger.debug("on_left_down in plot_panel %s. event: %s",
                          self.name, event)
        event.ResumePropagation(30)
        event.Skip()

    def set_color(self, rgbtuple=None):
        ''' Set figure and canvas colours to be the same.
        '''
        if rgbtuple is None:
            rgbtuple = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get()
        clr = [c / 255. for c in rgbtuple]
        self.figure.set_facecolor(clr)
        self.figure.set_edgecolor(clr)
        self.canvas.SetBackgroundColour(wx.Colour(*rgbtuple))
        self.canvas.Refresh()

    def draw(self):
        ''' Draw the canvas to make the changes visible.
        '''
        self.canvas.draw()

    def update_display(self):
        ''' Update the display of the docking frame.

        '''
        assert False, 'The update_display method must be defined!'