R.
#!/usr/bin/env pythonw
#### Standard Python import sys import os #### Installed Modules if 0: import wxversion wxversion.select('2.6.3.3') import wx import wx.lib.scrolledpanel as scrolled import matplotlib from matplotlib.numerix import * if 1: matplotlib.use('WXAgg') from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvasWx from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar2Wx else: matplotlib.use('WX') from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvasWx from matplotlib.backends.backend_wx import NavigationToolbar2Wx as NavigationToolbar2Wx from matplotlib.figure import Figure class AnalysisPlotFrame(wx.Frame): def __init__(self, parent, title=None): title = 'Untitled Plot Analysis' # Initialize the Frame base class wx.Frame.__init__(self,parent,-1,title,size=(550,350),name='plot') # bind the close event self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) # Create a place to store all x and y data for each axes and an X time variable self.Xt=10 #self.Xt=len(self.ct_file.saw_data[0]) # This will hold references to each axes created along with all of the data plotted self.axes_data = {} # a panel for all of the controls panel = scrolled.ScrolledPanel(self, -1) # Create the main figure plot_list = [(self.GetSinFigure,{})] cols_plot_list = [(self.GetSinFigure,{})] figure = Figure() base_plot_number = len(plot_list) * 100 + 10 #base_plot_number = len(cols_plot_list) * 100 + 20 for n, plot in enumerate(plot_list): figure_function, args = plot if not figure_function: continue print base_plot_number + n + 1 figure = figure_function(panel, subplot=base_plot_number + n + 1, figure=figure, **args) # Create the canvas self.canvas = FigureCanvasWx(panel, -1, figure) # Catch the event when the xaxis is changed on any of the axes so we can keep them # all together self.axes_list = figure.get_axes() def on_xlim_changed(axis): #print 'Axes updated' xlim = axis.get_xlim() self.set_all_xlim(xlim) # TODO: Insert that hack where we override the default axes.set_xlim with our own # For now, we have emit defaulting to True in axes.py for axis in self.axes_list: axis.connect('xlim_changed', on_xlim_changed) # Compute an x window range and set that xlim = self.ComputeXlim() self.set_all_xlim(xlim) # Create a sizer for the frame self.sizer = wx.BoxSizer(wx.VERTICAL) # Add the toolbar now so it will be at the top on Windows (no effect on Mac) self.add_toolbar(panel) # comment this out for no toolbar # Add the plots self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) # Create an update button which we'll use to step through the updates btn = wx.Button(panel, -1, 'Update') self.Bind(wx.EVT_BUTTON, self.OnUpdateButton, btn) btn.SetDefault() self.sizer.Add(btn, 0, wx.ALL, 10) panel.SetSizerAndFit(self.sizer) panel.SetupScrolling() # resize the frame to fit the children self.SetClientSize(panel.GetSize()) def set_all_xlim(self, xlim): for axis in self.axes_list: axis.set_xlim(xlim, emit=False) def ComputeXlim(self): width = 60 step = 0.01 window_size = width * step if (self.Xt * step) < window_size: xlim = [0, window_size] else: xlim = [(self.Xt * step) - window_size, (self.Xt * step)] print self.Xt, xlim return xlim def OnUpdateButton(self, event): incr = 170 self.Xt = self.Xt + incr xlim = self.ComputeXlim() self.set_all_xlim(xlim) for axes, plot_data in self.axes_data.items(): for line, xy in zip(axes.get_lines(), plot_data): line.set_data(xy[0][:self.Xt], xy[1][:self.Xt]) locator = axes.yaxis.get_major_locator() print axes.get_ylim() ylim = locator.autoscale() print ylim axes.set_ylim(ylim, emit=False) print axes.get_ylim() #if axes.get_autoscale_on(): #print 'Setting autoscale' #axes.set_autoscale_on(False) axes.autoscale_view(scalex=False, scaley=True) print axes.get_ylim() axes.autoscale_view(scalex=True, scaley=True) print axes.get_ylim() #axes.draw() self.canvas.draw() # TODO: Update the fills and horizontal bars self.Refresh() def add_toolbar(self, parent=None): if not parent: parent = self class NavigationToolbar2WxAgg_CT(NavigationToolbar2Wx): def press_pan(self, event): # Ignore the event if the 'alt' key is held down if event.key != 'alt': NavigationToolbar2Wx.press_pan(self, event) def press_zoom(self, event): # Ignore the event if the 'alt' key is held down if event.key != 'alt': NavigationToolbar2Wx.press_zoom(self, event) self.toolbar = NavigationToolbar2WxAgg_CT(self.canvas) self.toolbar.Realize() if parent==self and 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(self.toolbar) else: # On Windows platform, default window size is incorrect, so set # toolbar width to figure width. tw, th = self.toolbar.GetSizeTuple() fw, fh = self.canvas.GetSizeTuple() # 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. self.toolbar.SetSize(wx.Size(fw, th)) self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND) # update the axes menu on the toolbar self.toolbar.update() def OnCloseWindow(self, event): self.Destroy() def PushAxesEventData(self, axes_data, x, event_data): axes_data.append([x, event_data]) def GetSinFigure(self, parent=None, subplot=111, figure=None): if not parent: parent = self # To test the y-range autoscale, we'll have an ever increasing amplitude x = arange(0,20*3.14159,0.01) print len(x),(10-1.0)/len(x) amp = arange(1,10,(10-1.0)/len(x)) y = amp * sin(x) if figure == None: figure = Figure() axes = figure.add_subplot(subplot) axes_data = [] self.axes_data[axes] = axes_data self.PushAxesEventData(axes_data, x, y) lines = axes.plot(x[:self.Xt],y[:self.Xt]) axes.xaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%5.2f')) axes.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%5.2f')) axes.set_xlabel('Time (seconds)') axes.set_ylabel('Frequency shift (Hz)') axes.set_title('Test Plot') headings = ['sin(x)'] leg = axes.legend(lines, headings, 'lower right') leg.draw_frame(False) return figure class MyApp(wx.App): def OnInit(self): frame = AnalysisPlotFrame(None) self.SetTopWindow(frame) frame.Show(True) return True if __name__ == '__main__': if 1: app = MyApp(redirect=0) app.MainLoop()
On Sep 15, 2006, at 1:06 PM, John Hunter wrote:
"Richard" == Richard Harvey Chapman <hchapman-matplotlib- [EMAIL PROTECTED]> writes:Richard> I'm using the latest matplotlib with wxPython 2.6.2.1 and Richard> the WxAGG backend. I have plotted a figure with 5 Richard> subplots. I intentionally only plotted the first 10 Richard> points of all of my data traces so I can simulate the Richard> look of real-time data. That is, I keep my xlim set to Richard> (now-60, now) for a window showing the last 60 seconds Richard> only. For the y-axis, I'd like it to autoscale, but it Richard> never does. I've tried calling Richard> axes.autoscale_view(scalex=False, scaley=True) for each Richard> subplot, but with no effect. For each update, I take each Richard> line in each subplot and update the x and y data with Richard> set_data(). I just add a few points every few seconds. Am Richard> I missing something? Why can't I get the y autoscaling to Richard> work? Autoscaling is on by default, and I've called Richard> get_autoscale_on() to confirm it. Are you calling fig.canvas.draw() after the autoscale? If you are still having troubles, please post a complete example.... JDH
R.
PGP.sig
Description: This is a digitally signed message part
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users