def create_main_panel(self): # define the displays and controls self.init_plot() self.canvas = FigCanvas(self, -1, self.fig) self.create_control_bar() self.xmin_control = BoundControlBox(self, -1, "X min", 0) self.xmin_control.radio_auto.SetValue(True) self.xmax_control = BoundControlBox(self, -1, "X max", 50) self.xmax_control.radio_auto.SetValue(True) self.ymin_control = BoundControlBox(self, -1, "Y min", 0) self.ymin_control.radio_auto.SetValue(True) self.ymax_control = BoundControlBox(self, -1, "Y max", 100) self.ymax_control.radio_auto.SetValue(True) # lay displays and controls out on the page self.hbox2 = wx.BoxSizer(wx.HORIZONTAL) self.hbox2.Add(self.xmin_control, border=5, flag=wx.ALL) self.hbox2.Add(self.xmax_control, border=5, flag=wx.ALL) self.hbox2.Add(self.ymin_control, border=5, flag=wx.ALL) self.hbox2.Add(self.ymax_control, border=5, flag=wx.ALL) self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW) self.vbox.Add(self.hbox1, 0, flag=wx.ALIGN_LEFT | wx.TOP | wx.EXPAND) self.vbox.Add(self.hbox2, 0, flag=wx.ALIGN_LEFT | wx.TOP | wx.EXPAND) self.SetSizer(self.vbox) self.vbox.Fit(self)
class Graph(wx.Panel): def __init__(self,parent,datasource): wx.Panel.__init__(self, parent, -1) self.datagen = datasource self.create_main_panel() self.paused = True #self.start_worker() self.Bind(EVT_RESULT, self.on_result) def start_worker(self): self.worker = WorkerThread(self, self.datagen) self.worker.start() def create_control_bar(self): self.pause_button = wx.Button(self, -1, "Pause") self.Bind(wx.EVT_BUTTON, self.on_pause_button, self.pause_button) self.Bind(wx.EVT_UPDATE_UI, self.on_update_pause_button, self.pause_button) self.save_button = wx.Button(self, -1, "Save data") self.Bind(wx.EVT_BUTTON, self.on_save_button, self.save_button) self.hbox1 = wx.BoxSizer(wx.HORIZONTAL) self.hbox1.Add(self.pause_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL) self.hbox1.AddSpacer(20) self.hbox1.Add(self.save_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL) def create_main_panel(self): # define the displays and controls self.init_plot() self.canvas = FigCanvas(self, -1, self.fig) self.create_control_bar() self.xmin_control = BoundControlBox(self, -1, "X min", 0) self.xmin_control.radio_auto.SetValue(True) self.xmax_control = BoundControlBox(self, -1, "X max", 50) self.xmax_control.radio_auto.SetValue(True) self.ymin_control = BoundControlBox(self, -1, "Y min", 0) self.ymin_control.radio_auto.SetValue(True) self.ymax_control = BoundControlBox(self, -1, "Y max", 100) self.ymax_control.radio_auto.SetValue(True) # lay displays and controls out on the page self.hbox2 = wx.BoxSizer(wx.HORIZONTAL) self.hbox2.Add(self.xmin_control, border=5, flag=wx.ALL) self.hbox2.Add(self.xmax_control, border=5, flag=wx.ALL) self.hbox2.Add(self.ymin_control, border=5, flag=wx.ALL) self.hbox2.Add(self.ymax_control, border=5, flag=wx.ALL) self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW) self.vbox.Add(self.hbox1, 0, flag=wx.ALIGN_LEFT | wx.TOP | wx.EXPAND) self.vbox.Add(self.hbox2, 0, flag=wx.ALIGN_LEFT | wx.TOP | wx.EXPAND) self.SetSizer(self.vbox) self.vbox.Fit(self) def init_plot(self): self.fig = matplotlib.figure.Figure() self.axes = self.fig.add_subplot(111) x,y = self.datagen() self.lines = self.axes.plot(x,y) def on_result(self, event): if event.data is None: pass else: x,y = event.data self.update_plot(x,y) self.set_bounds() def update_plot(self, x, y): self.lines[0].set_data(x,y) self.axes.relim() self.axes.autoscale_view() def set_bounds(self): if not self.xmax_control.is_auto(): xmax = float(self.xmax_control.manual_value()) self.axes.set_xbound(upper = xmax) if not self.xmin_control.is_auto(): xmin = float(self.xmin_control.manual_value()) self.axes.set_xbound(lower = xmin) if not self.ymax_control.is_auto(): ymax = float(self.ymax_control.manual_value()) self.axes.set_ybound(upper = ymax) if not self.ymin_control.is_auto(): ymin = float(self.ymin_control.manual_value()) self.axes.set_ybound(lower = ymin) self.canvas.draw() # define event handlers def on_save_button(self, event): file_choices = "CSV (*.csv)|*.csv|Numpy (*.npy)|*.npy" dlg = wx.FileDialog( self, message="Save data as...", defaultDir = os.getcwd(), defaultFile = "data.csv", wildcard=file_choices, style=wx.SAVE|wx.OVERWRITE_PROMPT) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() # verify extension extensions = ('.csv', '.npy') ext = extensions[dlg.GetFilterIndex()] if type(path) is unicode: ext = unicode(ext) base, userext = os.path.splitext(path) if not userext == ext: path += ext if str(ext) == '.csv': self.save_csv(path) elif str(ext) == '.npy': self.save_npy(path) def save_csv(self, path): wrt = csv.writer(open(path,'w')) def cols(lines): for line in lines: yield line.get_xdata() yield line.get_ydata() for row in zip(*cols(self.lines)): wrt.writerow(row) def save_npy(self, path): lines = self.lines if len(lines) == 1: n.save(path, lines[0].get_data()) else: n.save(path, [l.get_data() for l in lines]) def on_pause_button(self,event): self.paused = not self.paused if not self.paused: self.start_worker() else: self.worker._want_abort = True def on_update_pause_button(self,event): label = "Resume" if self.paused else "Pause" self.pause_button.SetLabel(label)