From b57fd6a7bc585d4f9b57742f1245d4671e1c6685 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 1 Aug 2010 18:43:07 -0400 Subject: [PATCH] Make gui.panel.plot's toolbar optional & start generalizing axes selection --- hooke/ui/gui/panel/plot.py | 63 +++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/hooke/ui/gui/panel/plot.py b/hooke/ui/gui/panel/plot.py index e46c819..ab81c60 100644 --- a/hooke/ui/gui/panel/plot.py +++ b/hooke/ui/gui/panel/plot.py @@ -83,18 +83,19 @@ class PlotPanel (Panel, wx.Panel): self.display_coordinates = False self.style = 'line' self._curve = None + self._x_column = None + self._y_column = None super(PlotPanel, self).__init__( name='plot', callbacks=callbacks, **kwargs) self._c = {} self._c['figure'] = Figure() self._c['canvas'] = FigureCanvas( parent=self, id=wx.ID_ANY, figure=self._c['figure']) - self._c['toolbar'] = NavToolbar(self._c['canvas']) self._set_color(wx.NamedColor('WHITE')) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self._c['canvas'], 1, wx.LEFT | wx.TOP | wx.GROW) - self._setup_toolbar(toolbar=self._c['toolbar'], sizer=sizer) + self._setup_toolbar(sizer=sizer) # comment out to remove plot toolbar. self.SetSizer(sizer) self.Fit() @@ -108,13 +109,14 @@ class PlotPanel (Panel, wx.Panel): self._c['figure'].canvas.mpl_connect( 'motion_notify_event', self._on_mouse_move) - def _setup_toolbar(self, toolbar, sizer): + def _setup_toolbar(self, sizer): + self._c['toolbar'] = NavToolbar(self._c['canvas']) self._c['toolbar'].Realize() # call after putting items in the toolbar if wx.Platform == '__WXMAC__': # Mac platform (OSX 10.3, MacPython) does not seem to cope with # having a toolbar in a sizer. This work-around gets the buttons # back, but at the expense of having the toolbar at the top - self.SetToolBar(toolbar) + self.SetToolBar(self._c['toolbar']) elif wx.Platform == '__WXMSW__': # On Windows platform, default window size is incorrect, so set # toolbar width to figure width. @@ -123,10 +125,10 @@ class PlotPanel (Panel, wx.Panel): # By adding toolbar in sizer, we are able to put it at the bottom # of the frame - so appearance is closer to GTK version. # As noted above, doesn't work for Mac. - toolbar.SetSize(wx.Size(fw, th)) - sizer.Add(toolbar, 0 , wx.LEFT | wx.EXPAND) + self._c['toolbar'].SetSize(wx.Size(fw, th)) + sizer.Add(self._c['toolbar'], 0 , wx.LEFT | wx.EXPAND) else: - sizer.Add(toolbar, 0 , wx.LEFT | wx.EXPAND) + sizer.Add(self._c['toolbar'], 0 , wx.LEFT | wx.EXPAND) self._c['toolbar'].update() # update the axes menu on the toolbar def _set_color(self, rgbtuple=None): @@ -159,19 +161,29 @@ class PlotPanel (Panel, wx.Panel): #self.SetStatusText('') def _on_mouse_move(self, event): - if event.guiEvent.m_shiftDown: - self._c['toolbar'].set_cursor(wx.CURSOR_RIGHT_ARROW) - else: - self._c['toolbar'].set_cursor(wx.CURSOR_ARROW) + if 'toolbar' in self._c: + if event.guiEvent.m_shiftDown: + self._c['toolbar'].set_cursor(wx.CURSOR_RIGHT_ARROW) + else: + self._c['toolbar'].set_cursor(wx.CURSOR_ARROW) if self.display_coordinates: coordinateString = ''.join( ['x: ', str(event.xdata), ' y: ', str(event.ydata)]) #TODO: pretty format #self.SetStatusText(coordinateString) + def _on_x_column(self, event): + pass + + def _on_y_column(self, event): + pass + def _resize_canvas(self): w,h = self.GetClientSize() - tw,th = self._c['toolbar'].GetSizeTuple() + if 'toolbar' in self._c: + tw,th = self._c['toolbar'].GetSizeTuple() + else: + th = 0 dpi = float(self._c['figure'].get_dpi()) self._c['figure'].set_figwidth(w/dpi) self._c['figure'].set_figheight((h-th)/dpi) @@ -185,12 +197,17 @@ class PlotPanel (Panel, wx.Panel): def set_curve(self, curve, config={}): self._curve = curve + columns = set() + for data in curve.data: + columns = columns.union(set(data.info['columns'])) + self._columns = sorted(columns) + if self._x_column not in self._columns: + self._x_column = self._columns[0] + if self._y_column not in self._columns: + self._y_column = self._columns[-1] self.update(config=config) def update(self, config={}): - x_name = 'z piezo (m)' - y_name = 'deflection (m)' - self._c['figure'].clear() self._c['figure'].suptitle( self._hooke_frame._file_name(self._curve.name), @@ -199,26 +216,24 @@ class PlotPanel (Panel, wx.Panel): if config['plot SI format'] == 'True': # TODO: config should convert d = int(config['plot decimals']) # TODO: config should convert - x_n, x_unit = split_data_label(x_name) - y_n, y_unit = split_data_label(y_name) - fx = HookeFormatter(decimals=d)#, unit=x_unit) + x_n, x_unit = split_data_label(self._x_column) + y_n, y_unit = split_data_label(self._y_column) + fx = HookeFormatter(decimals=d, unit=x_unit) axes.xaxis.set_major_formatter(fx) fy = HookeFormatter(decimals=d, unit=y_unit) axes.yaxis.set_major_formatter(fy) axes.set_xlabel(x_n) axes.set_ylabel(y_n) else: - axes.set_xlabel(x_name) - axes.set_ylabel(y_name) + axes.set_xlabel(self._x_column) + axes.set_ylabel(self._y_column) self._c['figure'].hold(True) for i,data in enumerate(self._curve.data): - axes.plot(data[:,data.info['columns'].index(x_name)], - data[:,data.info['columns'].index(y_name)], + axes.plot(data[:,data.info['columns'].index(self._x_column)], + data[:,data.info['columns'].index(self._y_column)], '.', label=data.info['name']) if config['plot legend'] == 'True': # HACK: config should convert axes.legend(loc='best') self._c['canvas'].draw() - -# LocalWords: matplotlib -- 2.26.2