class TestFrame(wx.Frame): def __init__(self, parent=None, *args, **kwds): kwds[ "style"] = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.TAB_TRAVERSAL wx.Frame.__init__(self, parent, wx.NewId(), '', wx.DefaultPosition, wx.Size(-1, -1), **kwds) self.SetTitle(" WXMPlot Plotting Demo") self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False)) self.plotframe = None self.Bind(wx.EVT_TIMER, self.onTimer) self.timer = wx.Timer(self) self.Refresh() self.ShowPlotFrame(do_raise=False, clear=False) self.onStartTimer() def ShowPlotFrame(self, do_raise=True, clear=True): "make sure plot frame is enabled, and visible" if self.plotframe is None: self.plotframe = PlotFrame(self) self.has_plot = False try: self.plotframe.Show() except PyDeadObjectError: self.plotframe = PlotFrame(self) self.plotframe.Show() if do_raise: self.plotframe.Raise() if clear: self.plotframe.panel.clear() self.plotframe.reset_config() def onStartTimer(self, event=None): self.count = 0 self.timer.Start(1) def onTimer(self, event): # print 'timer ', self.count, time.time() self.count += 1 self.x = arange(0.0, 3, 0.01) self.y = sin(2 * pi * self.x + self.count) self.plotframe.plot(self.x, self.y) def OnExit(self, event): try: if self.plotframe != None: self.plotframe.onExit() except: pass self.Destroy()
class TestFrame(wx.Frame): def __init__(self, parent=None, *args, **kwds): kwds[ "style"] = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.TAB_TRAVERSAL wx.Frame.__init__(self, parent, -1, '', wx.DefaultPosition, wx.Size(-1, -1), **kwds) self.SetTitle(" WXMPlot Plotting Demo") self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False)) menu = wx.Menu() menu_exit = menu.Append(-1, "E&xit", "Terminate the program") menuBar = wx.MenuBar() menuBar.Append(menu, "&File") self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.OnExit, menu_exit) self.Bind(wx.EVT_CLOSE, self.OnExit) self.plotframe = None self.create_data() framesizer = wx.BoxSizer(wx.VERTICAL) panel = wx.Panel(self, -1, size=(-1, -1)) panelsizer = wx.BoxSizer(wx.VERTICAL) panelsizer.Add( wx.StaticText(panel, -1, 'wxmplot 2D PlotPanel examples '), 0, wx.ALIGN_LEFT | wx.LEFT | wx.EXPAND, 10) b10 = wx.Button(panel, -1, 'Example #1', size=(-1, -1)) b20 = wx.Button(panel, -1, 'Example #2', size=(-1, -1)) b22 = wx.Button(panel, -1, 'Plot with 2 axes', size=(-1, -1)) b31 = wx.Button(panel, -1, 'Plot with Errorbars', size=(-1, -1)) b32 = wx.Button(panel, -1, 'SemiLog Plot', size=(-1, -1)) b40 = wx.Button(panel, -1, 'Start Timed Plot', size=(-1, -1)) b50 = wx.Button(panel, -1, 'Stop Timed Plot', size=(-1, -1)) b60 = wx.Button(panel, -1, 'Plot 500,000 points', size=(-1, -1)) bmany1 = wx.Button(panel, -1, 'Plot 20 traces (delay_draw=False)', size=(-1, -1)) bmany2 = wx.Button(panel, -1, 'Plot 20 traces (delay_draw=True)', size=(-1, -1)) bmany3 = wx.Button(panel, -1, 'Plot 20 traces (use plot_many())', size=(-1, -1)) b10.Bind(wx.EVT_BUTTON, self.onPlot1) b20.Bind(wx.EVT_BUTTON, self.onPlot2) b22.Bind(wx.EVT_BUTTON, self.onPlot2Axes) b31.Bind(wx.EVT_BUTTON, self.onPlotErr) b32.Bind(wx.EVT_BUTTON, self.onPlotSLog) b40.Bind(wx.EVT_BUTTON, self.onStartTimer) b50.Bind(wx.EVT_BUTTON, self.onStopTimer) b60.Bind(wx.EVT_BUTTON, self.onPlotBig) bmany1.Bind(wx.EVT_BUTTON, self.onPlotMany_Slow) bmany2.Bind(wx.EVT_BUTTON, self.onPlotMany_Delay) bmany3.Bind(wx.EVT_BUTTON, self.onPlotMany_Fast) panelsizer.Add(b10, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b20, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b22, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b31, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b32, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b40, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b50, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b60, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(bmany1, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(bmany2, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(bmany3, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panel.SetSizer(panelsizer) panelsizer.Fit(panel) framesizer.Add(panel, 0, wx.ALIGN_LEFT | wx.EXPAND, 2) self.SetSizer(framesizer) framesizer.Fit(self) self.Bind(wx.EVT_TIMER, self.onTimer) self.timer = wx.Timer(self) self.Refresh() def create_data(self): self.count = 0 self.x = x = arange(0.0, 25.0, 0.1) self.y1 = 4 * cos(2 * pi * (x - 1) / 5.7) / (6 + x) + 2 * sin( 2 * pi * (x - 1) / 2.2) / (10) self.y2 = sin(2 * pi * x / 30.0) self.y3 = -pi + 2 * (x / 10. + exp(-(x - 3) / 5.0)) self.y4 = exp(0.01 + 0.5 * x) / (x + 2) self.y5 = 3000 * self.y3 self.npts = len(self.x) self.bigx = linspace(0, 2500, 500000) self.bigy = (sin(pi * self.bigx / 140.0) + cos(pi * self.bigx / 277.0) + cos(pi * self.bigx / 820.0)) self.many_dlist = [(self.x, self.y1)] for i in range(19): self.many_dlist.append((self.x, sin(2 * (i + 1) * x / 23.0))) def ShowPlotFrame(self, do_raise=True, clear=True): "make sure plot frame is enabled, and visible" if self.plotframe is None: self.plotframe = PlotFrame(self) self.has_plot = False try: self.plotframe.Show() except PyDeadObjectError: self.plotframe = PlotFrame(self) self.plotframe.Show() if do_raise: self.plotframe.Raise() if clear: self.plotframe.panel.clear() self.plotframe.reset_config() def onPlot1(self, event=None): self.ShowPlotFrame() self.plotframe.plot(self.x, self.y1) self.plotframe.oplot(self.x, self.y2) self.plotframe.write_message("Plot 1") def onPlot2(self, event=None): self.ShowPlotFrame() x = arange(100) y1 = cos(pi * x / 72) y2 = sin(pi * x / 23) self.plotframe.plot(x, y1, color='red') self.plotframe.oplot(x, y2, color='green3', marker='+') self.plotframe.write_message("Plot 2") def onPlotErr(self, event=None): self.ShowPlotFrame() npts = 81 x = linspace(0, 40.0, npts) y = 0.4 * cos(x / 2.0) + random.normal(scale=0.03, size=npts) dy = 0.03 * (ones(npts) + random.normal(scale=0.2, size=npts)) self.plotframe.plot(x, y, dy=dy, color='red', linewidth=0, xlabel='x', ylabel='y', marker='o', title='Plot with error bars') self.plotframe.write_message("Errorbars!") def onPlot2Axes(self, event=None): self.ShowPlotFrame() self.plotframe.plot(self.x, self.y2, color='black', style='dashed') self.plotframe.oplot(self.x, self.y5, color='red', side='right') self.plotframe.write_message("Plot with 2 axes") def onPlotSLog(self, event=None): self.ShowPlotFrame() self.plotframe.plot(self.x, self.y4, ylog_scale=True, color='black', style='dashed') self.plotframe.write_message("Semi Log Plot") def onPlotBig(self, event=None): self.ShowPlotFrame() t0 = time.time() self.plotframe.plot(self.bigx, self.bigy, marker='+', linewidth=0) dt = time.time() - t0 self.plotframe.write_message( "Plot array with npts=%i, elapsed time=%8.3f s" % (len(self.bigx), dt)) def onPlotMany_Slow(self, event=None): self.ShowPlotFrame() dlist = self.many_dlist t0 = time.time() opts = dict(title='Plot 20 traces without delay_draw', show_legend=True, xlabel='x') self.plotframe.plot(dlist[0][0], dlist[0][1], **opts) for tdat in dlist[1:]: self.plotframe.oplot(tdat[0], tdat[1]) dt = time.time() - t0 self.plotframe.write_message( "Plot 20 traces without delay_draw=True, elapsed time=%8.3f s" % (dt)) def onPlotMany_Delay(self, event=None): self.ShowPlotFrame() dlist = self.many_dlist t0 = time.time() opts = dict(title='Plot 20 traces with delay_draw', show_legend=True, xlabel='x') self.plotframe.plot(dlist[0][0], dlist[0][1], delay_draw=True, **opts) for tdat in dlist[1:-1]: self.plotframe.oplot(tdat[0], tdat[1], delay_draw=True) self.plotframe.oplot(dlist[-1][0], dlist[-1][1]) dt = time.time() - t0 self.plotframe.write_message( "Plot 20 traces with delay_draw=True, elapsed time=%8.3f s" % (dt)) def onPlotMany_Fast(self, event=None): self.ShowPlotFrame() dlist = self.many_dlist t0 = time.time() opts = dict(title='Plot 20 traces using plot_many()', show_legend=True, xlabel='x') self.plotframe.plot_many(dlist, **opts) dt = time.time() - t0 self.plotframe.write_message( "Plot 20 traces with plot_many(), elapsed time=%8.3f s" % (dt)) def report_memory(i): pid = os.getpid() if os.name == 'posix': mem = os.popen("ps -o rss -p %i" % pid).readlines()[1].split()[0] else: mem = 0 return int(mem) def onStartTimer(self, event=None): self.count = 0 self.up_count = 0 self.n_update = 1 self.datrange = None self.time0 = time.time() self.start_mem = self.report_memory() self.timer.Start(10) def timer_results(self): if (self.count < 2): return etime = time.time() - self.time0 tpp = etime / max(1, self.count) s = "drew %i points in %8.3f s: time/point= %8.4f s" % (self.count, etime, tpp) self.plotframe.write_message(s) self.time0 = 0 self.count = 0 self.datrange = None def onStopTimer(self, event=None): self.timer.Stop() try: self.timer_results() except: pass def onTimer(self, event): # print 'timer ', self.count, time.time() self.count += 1 n = self.count if n < 2: self.ShowPlotFrame(do_raise=False, clear=False) return if n >= self.npts: self.timer.Stop() self.timer_results() elif n <= 3: self.plotframe.plot(self.x[:n], self.y1[:n]) # , grid=False) else: self.plotframe.update_line(0, self.x[:n], self.y1[:n], update_limits=True, draw=True) etime = time.time() - self.time0 s = " %i / %i points in %8.4f s" % (n, self.npts, etime) self.plotframe.write_message(s) def OnAbout(self, event): dlg = wx.MessageDialog( self, "This sample program shows some\n" "examples of WXMPlot PlotFrame.\n" "message dialog.", "About WXMPlot test", wx.OK | wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy() def OnExit(self, event): try: if self.plotframe != None: self.plotframe.onExit() except: pass self.Destroy()
class ArlissMonitoringFrame(wx.Frame): title = 'Sensors Monitoring' def __init__(self): wx.Frame.__init__(self, None, -1, self.title, size=(700, 700)) os.system("banner ARLISS") #Partiendo la ventana en 2 paneles, en uno de ellos van los graficos ##en otro los botones y demas self.Maximize() #Funcion para abrir la ventana maximizada self.sp = wx.SplitterWindow(self) self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER) self.p2 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER) self.sp.SplitVertically(self.p1, self.p2, 250) self.keycode = '' self.Pause = False self.plotframe = None #Se crean los arreglos que van a guardar los datos self.dataAQ = DataAQ() #Clase que genera datos random, para pruebas self.datagen = DataGen() #Arreglo con los datos self.datos = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], []] self.datosGPS = [[], []] for line in open('GPS.txt', 'r'): self.datosGPS[0].append(line.split()[0]) self.datosGPS[1].append(line.split()[1]) #Se corren funciones para inicializar y crear paneles y el menu #En el panel es donde esta todo self.create_menu() self.create_status_bar() self.create_main_panel() #se inicializan los timers #Super importantes, cada 100ms ocurre un evento y se corre una funcion #en este caso: self.on_redraw_timer, la cual redibuja los graficos self.redraw_timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer) self.redraw_timer.Start(100) #Texto estatico wx.StaticText(self.p1, -1, "Latitud \t\t\t Longitud", (20, 80)) wx.StaticText(self.p1, -1, "Altura: ", (20, 605)) wx.StaticText(self.p1, -1, "Distancia: ", (20, 620)) wx.StaticText(self.p1, -1, "Estado: ", (20, 635)) wx.StaticText(self.p1, -1, "Moving ", (20, 650)) wx.StaticText(self.p1, -1, "Fix: ", (20, 665)) self.textoAltura = wx.StaticText(self.p1, -1, "0", (100, 605)) self.textoDistancia = wx.StaticText(self.p1, -1, "0", (100, 620)) self.textoEstado = wx.StaticText(self.p1, -1, "0", (100, 635)) self.textoMoviendose = wx.StaticText(self.p1, -1, "0", (100, 650)) self.textoFix = wx.StaticText(self.p1, -1, "0", (100, 665)) #Se crea una ventana para imprimir los datos de latitud y longitud self.logger = wx.TextCtrl(self.p1, 5, "", wx.Point(0, 100), wx.Size(250, 500), wx.TE_MULTILINE | wx.TE_READONLY) #Boton de prueba, cuando se clickea se crea un evento #y se corre una funcion self.pauseButton = wx.Button( self.p1, wx.ID_STOP, pos=(10, 25)) self.pauseButton.Bind(wx.EVT_BUTTON, self.onPause) self.saveButton = wx.Button( self.p1, wx.ID_SAVE, pos=(150, 25)) self.saveButton.Bind(wx.EVT_BUTTON, self.Save) #Funcion que se corre cada vez que se presiona el boton def Save(self, event): self.saveData(self) def onPause(self, event): if not self.Pause: self.Pause = True else: self.Pause = False def ShowPlotFrame(self, do_raise=True, clear=True): "make sure plot frame is enabled, and visible" if self.plotframe is None: self.plotframe = PlotFrame(self) self.has_plot = False try: self.plotframe.Show() except wx.PyDeadObjectError: self.plotframe = PlotFrame(self) self.plotframe.Show() if do_raise: self.plotframe.Raise() if clear: self.plotframe.panel.clear() self.plotframe.reset_config() #Creando el menu con todos las diferentes pesta;as y accesos def create_menu(self): self.menubar = wx.MenuBar() menu_file = wx.Menu() m_expt = menu_file.Append( -1, "&Save plot\tCtrl-S", "Save plot to file") self.Bind(wx.EVT_MENU, self.on_save_plot, m_expt) menu_file.AppendSeparator() m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit") self.Bind(wx.EVT_MENU, self.on_exit, m_exit) self.menubar.Append(menu_file, "&File") saveMenu = wx.Menu() m_Compass = saveMenu.Append(-1, "Plot Compass", "Plotear y guardar los datos de la brujula") self.Bind(wx.EVT_MENU, self.wxmPlotBrujula, m_Compass) m_CompassGoal = saveMenu.Append(-1, "Plot CompassGoal", "Plotear y guardar los datos del angulo con respecto a la meta") self.Bind(wx.EVT_MENU, self.wxmPlotBrujulaGoal, m_CompassGoal) m_PWM1 = saveMenu.Append(-1, "Plot PWM 1", "Plotear y guardar los datos del PWM del Motor 1") self.Bind(wx.EVT_MENU, self.wxmPlotPWM1, m_PWM1) m_PWM2 = saveMenu.Append(-1, "Plot PWM 2", "Plotear y guardar los datos del PWM del Motor 2") self.Bind(wx.EVT_MENU, self.wxmPlotPWM2, m_PWM2) m_V1 = saveMenu.Append(-1, "Plot V1", "Plotear y guardar los datos de la velocidad del Motor 1") self.Bind(wx.EVT_MENU, self.wxmPlotV1, m_V1) m_V2 = saveMenu.Append(-1, "Plot V2", "Plotear y guardar los datos de la velocidad del Motor 2") self.Bind(wx.EVT_MENU, self.wxmPlotV2, m_V2) m_GPS = saveMenu.Append(-1, "Google Earth GPS", "Mostrar en Google Earth los datos del GPS") self.Bind(wx.EVT_MENU, self.googleEarth, m_GPS) m_Altitud = saveMenu.Append(-1, "Plot Altitud", "Plotear y guardar los datos de la Altitud") self.Bind(wx.EVT_MENU, self.wxmPlotAltitud, m_Altitud) m_Target = saveMenu.Append(-1, "Plot Target", "Plotear y guardar los datos de la distancia de la meta") self.Bind(wx.EVT_MENU, self.wxmPlotTarget, m_Target) #Dejemos en standby lo de posX y posY m_Estado = saveMenu.Append(-1, "Plot Estado", "Plotear y guardar los datos del Estado del Rover") self.Bind(wx.EVT_MENU, self.wxmPlotEstado, m_Estado) m_Moviendose = saveMenu.Append(-1, "Plot Moviendose", "Plotear y guardar los datos del Estado del Movimiento del Rover") self.Bind(wx.EVT_MENU, self.wxmPlotMoviendose, m_Moviendose) m_Fix = saveMenu.Append(-1, "Plot GPS Fix", "Plotear y guardar los datos del Fix del GPS") self.Bind(wx.EVT_MENU, self.wxmPlotFix, m_Fix) self.menubar.Append(saveMenu, "Plotear Datos") self.SetMenuBar(self.menubar) #Se crea un panel, el q va a contener los graficos, se inicializan los #graficos y se imprimen en el panel self.p2 como una figura. def create_main_panel(self): self.init_plot() self.canvas = FigCanvas(self.p2, -1, self.fig) #Se crea la barra de estado que esta en la parte de abajo de la ventana def create_status_bar(self): self.statusbar = self.CreateStatusBar() #Texto que va en la barra de estado, se puede #cambiar por medio de eventos self.statusbar.SetStatusText("Sensors Monitoring") #Inicializando los graficos, aqui se utiliza mayormente #la libreria matplotlib #Se indican las propiedades de los graficos y otras cosas def init_plot(self): #Resolucion del grafico self.dpi = 100 #Se crea el objeto que va a tener el o los graficos, #se le indica la resolucion #y el tamano self.fig = Figure((11, 7.0), dpi=self.dpi) #Se le agrega un subplot a la figura llamada PWM, #Esta figura va a tener los datos de los 2 PWM #se indica que la figura #va a tener un arreglo de graficos 2x2 (fila x columna) y que subplot #PWM va a ser el primero de los dos subplots. self.PWM = self.fig.add_subplot(221) self.PWM2 = self.fig.add_subplot(221) self.PWM.set_axis_bgcolor('black') self.PWM.set_title('PWM', size=12) pylab.setp(self.PWM.get_xticklabels(), fontsize=8) pylab.setp(self.PWM.get_yticklabels(), fontsize=8) #Se plotean datos, pero por primera vez, #luego se actualizan con el timer self.plot_PWM = self.PWM.plot( self.datos[PWM1], linewidth=1, color=(1, 1, 0),)[0] self.plot_PWM2 = self.PWM2.plot( (self.datos[PWM2]), linewidth=1, color=(1, 0, 0),)[0] #Falta PWM2 self.PWM.legend([self.plot_PWM, self.plot_PWM2], ["PWM1", "PWM2"]) #Agregando plot de Brujula #Aqui van a ir ploteados los datos del angulo con respecto al norte #Y el angulo con respecto al goal self.Brujula = self.fig.add_subplot(222) self.BrujulaGoal = self.fig.add_subplot(222) self.Brujula.set_axis_bgcolor('black') self.Brujula.set_title('Angulos', size=12) pylab.setp(self.Brujula.get_xticklabels(), fontsize=8) pylab.setp(self.Brujula.get_yticklabels(), fontsize=8) self.plot_Brujula = self.Brujula.plot( self.datos[COMPASS], linewidth=1, color=(0, 0, 1),)[0] self.plot_BrujulaGoal = self.BrujulaGoal.plot( self.datos[COMPASSGOAL], linewidth=1, color=(1, 0, 1),)[0] self.Brujula.legend([self.plot_Brujula, self.plot_BrujulaGoal], ["Norte", "Meta"]) #Agregando plot de Velocidades self.Vel = self.fig.add_subplot(223) self.Vel2 = self.fig.add_subplot(223) self.Vel.set_axis_bgcolor('black') self.Vel.set_title('Velocidades', size=12) pylab.setp(self.Vel.get_xticklabels(), fontsize=8) pylab.setp(self.Vel.get_yticklabels(), fontsize=8) self.plot_Vel = self.Vel.plot( self.datos[V1], linewidth=1, color=(0, 1, 0),)[0] self.plot_Vel2 = self.Vel2.plot( self.datos[V2], linewidth=1, color=(1, 1, 0),)[0] self.Vel.legend([self.plot_Vel, self.plot_Vel2], ["M1", "M2"]) #Agregando plot de POS self.POS = self.fig.add_subplot(224) self.POS.set_axis_bgcolor('black') self.POS.set_title('Posiciones', size=12) pylab.setp(self.POS.get_xticklabels(), fontsize=8) pylab.setp(self.POS.get_yticklabels(), fontsize=8) self.plot_POS = self.POS.plot( self.datos[POSX], self.datos[POSY], linewidth=1, color=(0, 0, 1),)[0] def draw_plot(self): xmax_PWM = len(self.datos[PWM1]) if len(self.datos[PWM1]) > 50 else 50 xmin_PWM = xmax_PWM - 50 ymin_PWM = round(min(min(self.datos[PWM1]), min(self.datos[PWM2])), 0) - 1 ymax_PWM = round(max(max(self.datos[PWM1]), max(self.datos[PWM2])), 0) + 1 #SACAR EL MINIMO Y MAX DE 2 ARRAYS, PWM1 Y PWM2 self.PWM.set_xbound(lower=xmin_PWM, upper=xmax_PWM) self.PWM.set_ybound(lower=ymin_PWM, upper=ymax_PWM) self.PWM.grid(True, color='w') self.plot_PWM.set_xdata(np.arange(len(self.datos[PWM1]))) self.plot_PWM.set_ydata(np.array(self.datos[PWM1])) self.plot_PWM2.set_xdata(np.arange(len(self.datos[PWM2]))) self.plot_PWM2.set_ydata(np.array(self.datos[PWM2])) #Plot de Brujula xmax_Comp = len(self.datos[COMPASS]) \ if len(self.datos[COMPASS]) > 50 else 50 xmin_Comp = xmax_Comp - 50 ymin_Comp = round(min(min(self.datos[COMPASS]), min(self.datos[COMPASSGOAL])), 0) + 1 ymax_Comp = round(max(max(self.datos[COMPASS]), max(self.datos[COMPASSGOAL])), 0) + 1 #SACAR EL MINIMO DE COMPASS Y COMPASSGOAL self.Brujula.set_xbound(lower=xmin_Comp, upper=xmax_Comp) self.Brujula.set_ybound(lower=ymin_Comp, upper=ymax_Comp) self.Brujula.grid(True, color='w') self.plot_Brujula.set_xdata(np.arange(len(self.datos[COMPASS]))) self.plot_Brujula.set_ydata(np.array(self.datos[COMPASS])) self.plot_BrujulaGoal.set_xdata(np.arange(len(self.datos[COMPASSGOAL]))) self.plot_BrujulaGoal.set_ydata(np.array(self.datos[COMPASSGOAL])) #Plot de Velocidades xmax_Vel = len(self.datos[V1]) \ if len(self.datos[V1]) > 50 else 50 xmin_Vel = xmax_Vel - 50 ymin_Vel = round(min(min(self.datos[V1]), min(self.datos[V2])), 0) - 1 ymax_Vel = round(max(max(self.datos[V1]), max(self.datos[V2])), 0) + 1 self.Vel.set_xbound(lower=xmin_Vel, upper=xmax_Vel) self.Vel.set_ybound(lower=ymin_Vel, upper=ymax_Vel) self.Vel.grid(True, color='w') self.plot_Vel.set_xdata(np.arange(len(self.datos[V1]))) self.plot_Vel.set_ydata(np.array(self.datos[V1])) self.plot_Vel2.set_xdata(np.arange(len(self.datos[V2]))) self.plot_Vel2.set_ydata(np.array(self.datos[V2])) #Plot de POS xmax_POS = round(max(self.datos[POSX]), 0) - 1 xmin_POS = round(min(self.datos[POSX]), 0) - 1 ymin_POS = round(min(self.datos[POSY]), 0) - 1 ymax_POS = round(max(self.datos[POSY]), 0) + 1 self.POS.set_xbound(lower=xmin_POS, upper=xmax_POS) self.POS.set_ybound(lower=ymin_POS, upper=ymax_POS) self.POS.grid(True, color='w') self.plot_POS.set_xdata(np.array(self.datos[POSX])) self.plot_POS.set_ydata(np.array(self.datos[POSY])) #Dibujando Plot self.canvas.draw() def on_save_plot(self, event): file_choices = "PNG (*.png)|*.png" dlg = wx.FileDialog( self, message="Save plot as...", defaultDir=os.getcwd(), defaultFile="plot.png", wildcard=file_choices, style=wx.SAVE) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() self.canvas.print_figure(path, dpi=self.dpi) self.flash_status_message("Saved to %s" % path) def on_redraw_timer(self, event): wx.Yield() if not self.Pause: tempDatos = self.dataAQ.get() for i in range(15): self.datos[i].append(tempDatos[i]) self.logger.AppendText(("%0.10f" % tempDatos[LATITUD]) + "\t" + ("%0.10f" % tempDatos[LONGITUD]) + "\n") self.textoAltura.SetLabel(str(tempDatos[ALTITUD])) self.textoDistancia.SetLabel(str(tempDatos[TARGET])) self.textoEstado.SetLabel(str(tempDatos[ESTADO])) self.textoMoviendose.SetLabel(str(tempDatos[MOVIENDOSE])) self.textoFix.SetLabel(str(tempDatos[FIX])) self.draw_plot() def on_exit(self, event): self.Destroy() def getPath(self): strTime = (str(time.localtime()[0]) + "-" + str(time.localtime()[1]) + "-" + str(time.localtime()[2]) + "_" + str(time.localtime()[3]) + ":" + str(time.localtime()[4])) path = 'Datos_{}'.format(strTime) return path def saveData(self, event): #En esta funcion se van a guardar todos los datos que entran en un .txt #Los datos son: #COMPASS, PWM1, PWM2, V1, V2, LATITUD, LONGITUD, ALTITUD, COMPASSGOAL, #TARGET, POSX, POSY, ESTADO, MOVIENDOSE, FIX newpath = self.getPath() if not os.path.exists(newpath): os.makedirs(newpath) brujulaPath = newpath + "/datos_Brujula.txt" brujulaGoalPath = newpath + "/datos_BrujulaGoal.txt" PWM1Path = newpath + "/datos_PWM1.txt" PWM2Path = newpath + "/datos_PWM2.txt" V1Path = newpath + "/datos_V1.txt" V2Path = newpath + "/datos_V2.txt" GPSPath = newpath + "/datos_GPS.txt" self.GPSKMLPath = newpath + "/datos_GPS.kml" AltitudPath = newpath + "/datos_Altitud.txt" TargetPath = newpath + "/datos_Target.txt" PosPath = newpath + "/datos_POS.txt" EstadoPath = newpath + "/datos_Estado.txt" MoviendosePath = newpath + "/datos_Moviendose.txt" FixPath = newpath + "/datos_Fix.txt" self.fileBrujula = open(brujulaPath, 'w') self.fileBrujula.write("#Datos de Brujula\n") for i in range(len(self.datos[COMPASS])): txt = str(i) + '\t' + str(self.datos[COMPASS][i]) + '\n' self.fileBrujula.write(txt) self.fileBrujula.close() self.fileBrujulaGoal = open(brujulaGoalPath, 'w') self.fileBrujulaGoal.write("#Datos de BrujulaGoal\n") for i in range(len(self.datos[COMPASSGOAL])): txt = str(i) + '\t' + str(self.datos[COMPASSGOAL][i]) + '\n' self.fileBrujulaGoal.write(txt) self.fileBrujulaGoal.close() self.filePWM1 = open(PWM1Path, 'w') self.filePWM1.write("#Datos de PWM de Motor 1\n") for i in range(len(self.datos[PWM1])): txt = str(i) + '\t' + str(self.datos[PWM1][i]) + '\n' self.filePWM1.write(txt) self.filePWM1.close() self.filePWM2 = open(PWM2Path, 'w') self.filePWM2.write("#Datos de PWM de Motor 2\n") for i in range(len(self.datos[PWM2])): txt = str(i) + '\t' + str(self.datos[PWM2][i]) + '\n' self.filePWM2.write(txt) self.filePWM2.close() self.fileV1 = open(V1Path, 'w') self.fileV1.write("#Datos de velocidad del motor 1\n") for i in range(len(self.datos[V1])): txt = str(i) + '\t' + str(self.datos[V1][i]) + '\n' self.fileV1.write(txt) self.fileV1.close() self.fileV2 = open(V2Path, 'w') self.fileV2.write("#Datos de Velocidad del motor 2\n") for i in range(len(self.datos[V2])): txt = str(i) + '\t' + str(self.datos[V2][i]) + '\n' self.fileV2.write(txt) self.fileV2.close() self.fileAltitud = open(AltitudPath, 'w') self.fileAltitud.write("#Datos de Altitud dada por el barometro\n") for i in range(len(self.datos[ALTITUD])): txt = str(i) + '\t' + str(self.datos[ALTITUD][i]) + '\n' self.fileAltitud.write(txt) self.fileAltitud.close() self.fileTarget = open(TargetPath, 'w') self.fileTarget.write("#Datos de la distancia de la meta\n") for i in range(len(self.datos[TARGET])): txt = str(i) + '\t' + str(self.datos[TARGET][i]) + '\n' self.fileTarget.write(txt) self.fileTarget.close() self.filePOS = open(PosPath, 'w') self.filePOS.write("#Datos de la posicion en X y en Y\n") for i in range(len(self.datos[POSX])): txt = str(self.datos[POSX][i]) + \ '\t' + str(self.datos[POSY][i]) + '\n' self.filePOS.write(txt) self.filePOS.close() self.fileEstado = open(EstadoPath, 'w') self.fileEstado.write("#Datos del estado del Rover\n") for i in range(len(self.datos[ESTADO])): txt = str(i) + '\t' + str(self.datos[ESTADO][i]) + '\n' self.fileEstado.write(txt) self.fileEstado.close() self.fileMoviendose = open(MoviendosePath, 'w') self.fileMoviendose.write("#Datos binarios del movimiento del Rover\n") for i in range(len(self.datos[MOVIENDOSE])): txt = str(i) + '\t' + str(self.datos[MOVIENDOSE][i]) + '\n' self.fileMoviendose.write(txt) self.fileMoviendose.close() self.fileFix = open(FixPath, 'w') self.fileFix.write("#Datos del Fix del GPS\n") for i in range(len(self.datos[FIX])): txt = str(i) + '\t' + str(self.datos[FIX][i]) + '\n' self.fileFix.write(txt) self.fileFix.close() self.fileGPS = open(GPSPath, 'w') self.fileGPS.write("#Datos del GPS\n") for i in range(len(self.datos[LATITUD])): txt = str(self.datos[LATITUD][i]) +\ '\t' + str(self.datos[LONGITUD][i]) + '\n' self.fileGPS.write(txt) self.fileGPS.close() #GUARDANDO DATOS EN .KML kml = '<Placemark><LineString><coordinates>' for i in range(len(self.datosGPS[1])): kml += '\n' + str(self.datosGPS[1][i]) + \ ',' + str(self.datosGPS[0][i]) #for i in range(len(self.datos[LATITUD])): #kml += '\n' + str(self.datos[LONGITUD][i]) + \ #',' + str(self.datos[LATITUD][i]) kml += '\n </coordinates></LineString></Placemark>' with open(self.GPSKMLPath, 'w+') as data_file: data_file.write(kml) data_file.flush() self.flash_status_message("Guardado en %s" % newpath) def googleEarth(self, event): os.system("banner GOOGLE EARTH") command = "gnome-open " + self.GPSKMLPath os.system(command) def wxmPlotBrujula(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[COMPASS]), 1) self.plotframe.plot(ndato, self.datos[COMPASS], color='red', title='Datos Brujula') def wxmPlotBrujulaGoal(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[COMPASSGOAL]), 1) self.plotframe.plot(ndato, self.datos[COMPASSGOAL], color='red', title='Datos BrujulaGoal') def wxmPlotPWM1(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[PWM1]), 1) self.plotframe.plot(ndato, self.datos[PWM1], color='red', title='Datos PWM 1') def wxmPlotPWM2(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[PWM2]), 1) self.plotframe.plot(ndato, self.datos[PWM2], color='red', title='Datos PWM 2') def wxmPlotV1(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[V1]), 1) self.plotframe.plot(ndato, self.datos[V1], color='red', title='Datos V1') def wxmPlotV2(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[V2]), 1) self.plotframe.plot(ndato, self.datos[V2], color='red', title='Datos V2') def wxmPlotAltitud(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[ALTITUD]), 1) self.plotframe.plot(ndato, self.datos[ALTITUD], color='red', title='Datos Altitud') def wxmPlotTarget(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[TARGET]), 1) self.plotframe.plot(ndato, self.datos[TARGET], color='red', title='Datos distancia de la Meta') def wxmPlotEstado(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[ESTADO]), 1) self.plotframe.plot(ndato, self.datos[ESTADO], color='red', title='Datos Estado del Rover') def wxmPlotMoviendose(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[MOVIENDOSE]), 1) self.plotframe.plot(ndato, self.datos[MOVIENDOSE], color='red', title='Datos Estado del movimiento del Rover') def wxmPlotFix(self, event): self.ShowPlotFrame() ndato = np.arange(0, len(self.datos[FIX]), 1) self.plotframe.plot(ndato, self.datos[FIX], color='red', title='Datos del FIX del GPS') def flash_status_message(self, msg, flash_len_ms=1500): self.statusbar.SetStatusText(msg) self.timeroff = wx.Timer(self) self.Bind( wx.EVT_TIMER, self.on_flash_status_off, self.timeroff) self.timeroff.Start(flash_len_ms, oneShot=True) def on_flash_status_off(self, event): self.statusbar.SetStatusText('Sensors Monitoring')
class TestFrame(wx.Frame): def __init__(self, parent=None, *args,**kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE|wx.RESIZE_BORDER|wx.TAB_TRAVERSAL wx.Frame.__init__(self, parent, wx.NewId(), '', wx.DefaultPosition, wx.Size(-1,-1), **kwds) self.SetTitle("signal test") self.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.BOLD,False)) self.plotframe = None self.Bind(wx.EVT_TIMER, self.onTimer) self.timer = wx.Timer(self) #slider self.sld = wx.Slider(self, value = 973, minValue = 800, maxValue = 1100, style = wx.SL_HORIZONTAL) self.sld.Bind(wx.EVT_SLIDER, self.OnSliderScroll) self.Refresh() self.ShowPlotFrame(do_raise=False, clear=False) self.onStartTimer() def ShowPlotFrame(self, do_raise=True, clear=True): "make sure plot frame is enabled, and visible" if self.plotframe is None: self.plotframe = PlotFrame(self) self.has_plot = False try: self.plotframe.Show() except PyDeadObjectError: self.plotframe = PlotFrame(self) self.plotframe.Show() if do_raise: self.plotframe.Raise() if clear: self.plotframe.panel.clear() self.plotframe.reset_config() def onStartTimer(self,event=None): self.count = 0 self.timer.Start(1) def onTimer(self, event): # print 'timer ', self.count, time.time() self.count += 1 N_Samples = 256 * 1024 samples = sdr.read_samples(N_Samples) interval = 2048 N = N_Samples//interval * interval y = samples[:N] y = y[:len(y//interval*interval)] y = y.reshape(N//interval, interval) y_windowed = y*np.kaiser(interval, 6) Y = fftshift(fft(y_windowed,axis=1),axes=1) Pspect = np.mean(abs(Y)*abs(Y),axis=0); self.plotframe.plot(np.arange(0, len(Pspect), 1),Pspect) def OnExit(self, event): try: if self.plotframe != None: self.plotframe.onExit() except: pass self.Destroy() def OnSliderScroll(self, e): obj = e.GetEventObject() val = obj.GetValue() sdr.center_freq = val * 1e5 print("frequency: "+str(sdr.center_freq / 10e6)+"MHz")
class TestFrame(wx.Frame): def __init__(self, parent=None, *args, **kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.TAB_TRAVERSAL wx.Frame.__init__(self, parent, wx.NewId(), "", wx.DefaultPosition, wx.Size(-1, -1), **kwds) self.SetTitle(" WXMPlot Plotting Demo") self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False)) menu = wx.Menu() ID_EXIT = wx.NewId() menu_exit = menu.Append(ID_EXIT, "E&xit", "Terminate the program") menuBar = wx.MenuBar() menuBar.Append(menu, "&File") self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.OnExit, menu_exit) self.Bind(wx.EVT_CLOSE, self.OnExit) # CloseEvent) self.plotframe = None self.create_data() framesizer = wx.BoxSizer(wx.VERTICAL) panel = wx.Panel(self, -1, size=(-1, -1)) panelsizer = wx.BoxSizer(wx.VERTICAL) panelsizer.Add( wx.StaticText(panel, -1, "wxmplot 2D PlotPanel examples "), 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT | wx.EXPAND, 10, ) b10 = wx.Button(panel, -1, "Example #1", size=(-1, -1)) b20 = wx.Button(panel, -1, "Example #2", size=(-1, -1)) b22 = wx.Button(panel, -1, "Plot with 2 axes", size=(-1, -1)) b31 = wx.Button(panel, -1, "Plot with Errorbars", size=(-1, -1)) b32 = wx.Button(panel, -1, "SemiLog Plot", size=(-1, -1)) b40 = wx.Button(panel, -1, "Start Timed Plot", size=(-1, -1)) b50 = wx.Button(panel, -1, "Stop Timed Plot", size=(-1, -1)) b60 = wx.Button(panel, -1, "Plot 500,000 points", size=(-1, -1)) b10.Bind(wx.EVT_BUTTON, self.onPlot1) b20.Bind(wx.EVT_BUTTON, self.onPlot2) b22.Bind(wx.EVT_BUTTON, self.onPlot2Axes) b31.Bind(wx.EVT_BUTTON, self.onPlotErr) b32.Bind(wx.EVT_BUTTON, self.onPlotSLog) b40.Bind(wx.EVT_BUTTON, self.onStartTimer) b50.Bind(wx.EVT_BUTTON, self.onStopTimer) b60.Bind(wx.EVT_BUTTON, self.onPlotBig) panelsizer.Add(b10, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b20, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b22, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b31, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b32, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b40, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b50, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panelsizer.Add(b60, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panel.SetSizer(panelsizer) panelsizer.Fit(panel) framesizer.Add(panel, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.EXPAND, 2) self.SetSizer(framesizer) framesizer.Fit(self) self.Bind(wx.EVT_TIMER, self.onTimer) self.timer = wx.Timer(self) self.Refresh() def create_data(self): self.count = 0 self.x = x = arange(0.0, 25.0, 0.1) self.y1 = 4 * cos(2 * pi * (x - 1) / 5.7) / (6 + x) + 2 * sin(2 * pi * (x - 1) / 2.2) / (10) self.y2 = sin(2 * pi * x / 30.0) self.y3 = -pi + 2 * (x / 10.0 + exp(-(x - 3) / 5.0)) self.y4 = exp(0.01 + 0.5 * x) / (x + 2) self.y5 = 3000 * self.y3 self.npts = len(self.x) self.bigx = linspace(0, 2500, 500000) self.bigy = sin(pi * self.bigx / 140.0) + cos(pi * self.bigx / 277.0) + cos(pi * self.bigx / 820.0) def ShowPlotFrame(self, do_raise=True, clear=True): "make sure plot frame is enabled, and visible" if self.plotframe is None: self.plotframe = PlotFrame(self) self.has_plot = False try: self.plotframe.Show() except PyDeadObjectError: self.plotframe = PlotFrame(self) self.plotframe.Show() if do_raise: self.plotframe.Raise() if clear: self.plotframe.panel.clear() self.plotframe.reset_config() def onPlot1(self, event=None): self.ShowPlotFrame() self.plotframe.plot(self.x, self.y1) self.plotframe.oplot(self.x, self.y2) self.plotframe.write_message("Plot 1") def onPlot2(self, event=None): self.ShowPlotFrame() x = arange(100) y1 = cos(pi * x / 72) y2 = sin(pi * x / 23) self.plotframe.plot(x, y1, color="red") self.plotframe.oplot(x, y2, color="green3", marker="+") self.plotframe.write_message("Plot 2") def onPlotErr(self, event=None): self.ShowPlotFrame() npts = 81 x = linspace(0, 40.0, npts) y = 0.4 * cos(x / 2.0) + random.normal(scale=0.03, size=npts) dy = 0.03 * (ones(npts) + random.normal(scale=0.2, size=npts)) self.plotframe.plot( x, y, dy=dy, color="red", linewidth=0, xlabel="x", ylabel="y", marker="o", title="Plot with error bars" ) self.plotframe.write_message("Errorbars!") def onPlot2Axes(self, event=None): self.ShowPlotFrame() self.plotframe.plot(self.x, self.y2, color="black", style="dashed") self.plotframe.oplot(self.x, self.y5, color="red", side="right") self.plotframe.write_message("Plot with 2 axes") def onPlotSLog(self, event=None): self.ShowPlotFrame() self.plotframe.plot(self.x, self.y4, ylog_scale=True, color="black", style="dashed") self.plotframe.write_message("Semi Log Plot") def onPlotBig(self, event=None): self.ShowPlotFrame() t0 = time.time() self.plotframe.plot(self.bigx, self.bigy, marker="+", linewidth=0) dt = time.time() - t0 self.plotframe.write_message("Plot array with npts=%i, elapsed time=%8.3f s" % (len(self.bigx), dt)) def report_memory(i): pid = os.getpid() if os.name == "posix": mem = os.popen("ps -o rss -p %i" % pid).readlines()[1].split()[0] else: mem = 0 return int(mem) def onStartTimer(self, event=None): self.count = 0 self.up_count = 0 self.n_update = 1 self.datrange = None self.time0 = time.time() self.start_mem = self.report_memory() self.timer.Start(10) def timer_results(self): if self.count < 2: return etime = time.time() - self.time0 tpp = etime / max(1, self.count) s = "drew %i points in %8.3f s: time/point= %8.4f s" % (self.count, etime, tpp) self.plotframe.write_message(s) self.time0 = 0 self.count = 0 self.datrange = None def onStopTimer(self, event=None): self.timer.Stop() try: self.timer_results() except: pass def onTimer(self, event): # print 'timer ', self.count, time.time() self.count += 1 self.ShowPlotFrame(do_raise=False, clear=False) n = self.count if n < 2: return if n >= self.npts: self.timer.Stop() self.timer_results() elif n <= 3: self.plotframe.plot(self.x[:n], self.y1[:n]) # , grid=False) else: self.plotframe.update_line(0, self.x[:n], self.y1[:n], update_limits=n < 10, draw=True) etime = time.time() - self.time0 s = " %i / %i points in %8.4f s" % (n, self.npts, etime) self.plotframe.write_message(s) if self.datrange is None: self.datrange = [min(self.x[:n]), max(self.x[:n]), min(self.y1[:n]), max(self.y1[:n])] dr = [min(self.x[:n]), max(self.x[:n]), min(self.y1[:n]), max(self.y1[:n])] lims = self.plotframe.panel.get_viewlimits() if dr[0] < lims[0] or dr[1] > lims[1] or dr[2] < lims[2] or dr[3] > lims[3]: self.datrange = dr if n < len(self.x): nmax = min(int(n * 1.6), len(self.x) - 1) self.datrange[1] = self.x[nmax] self.plotframe.panel.set_xylims(self.datrange) # if (n > self.n_update-1) or # (xr[0] < xv[0]) or (xr[1] > xv[1]) or # (yr[0] < yv[0]) or (yr[1] > yv[1])): # nx = self.n_update = min(self.npts,3+int(self.n_update*2.0)) # if nx > int(0.92*self.npts): # nx = self.n_update = self.npts # xylims = (min(self.x),max(self.x), # min(self.y1),max(self.y1)) # else: # xylims = (min(self.x[0:nx]),max(self.x[0:nx]), # min(self.y1[0:nx]),max(self.y1[0:nx])) # self.up_count = self.up_count + 1 # self.plotframe.panel.set_xylims(xylims) def OnAbout(self, event): dlg = wx.MessageDialog( self, "This sample program shows some\n" "examples of MPlot PlotFrame.\n" "message dialog.", "About MPlot test", wx.OK | wx.ICON_INFORMATION, ) dlg.ShowModal() dlg.Destroy() def OnExit(self, event): try: if self.plotframe != None: self.plotframe.onExit() except: pass self.Destroy()
class XAFSviewerFrame(wx.Frame): def __init__(self, parent=None, *args, **kwds): #kwds["style"] = wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.TAB_TRAVERSAL wx.Frame.__init__(self, parent, wx.NewId(), '', wx.DefaultPosition, wx.Size(-1, -1), **kwds) #self.SetTitle(" WXMPlot Plotting Demo") """ self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False)) menu = wx.Menu() ID_EXIT = wx.NewId() ID_TIMER = wx.NewId() menu.Append(ID_EXIT, "E&xit", "Terminate the program") menuBar = wx.MenuBar() menuBar.Append(menu, "&File") self.SetMenuBar(menuBar) wx.EVT_MENU(self, ID_EXIT, self.OnExit) self.Bind(wx.EVT_CLOSE, self.OnExit) """ self.plotframe = None """ framesizer = wx.BoxSizer(wx.VERTICAL) panel = wx.Panel(self, -1, size=(-1, -1)) panelsizer = wx.BoxSizer(wx.VERTICAL) panelsizer.Add(wx.StaticText(panel, -1, 'XAFS Spectra viewer '), 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT | wx.EXPAND, 10) b40 = wx.Button(panel, -1, 'Start Timed Plot', size=(-1, -1)) #b40.Bind(wx.EVT_BUTTON,self.onStartTimer) panelsizer.Add(b40, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.LEFT, 5) panel.SetSizer(panelsizer) panelsizer.Fit(panel) framesizer.Add(panel, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER | wx.EXPAND, 2) self.SetSizer(framesizer) framesizer.Fit(self) """ #----------- added by LWW ----------------------------- #====== self.showPlotFrame(do_raise=False, clear=False) """ self.plotYdata = [] self.plotXdata = [] # ----------epics PVs----------------------- self.i0 = PV('BL10C:scaler1_calc2.VAL') self.it = PV('BL10C:scaler1_calc3.VAL') self.iF = PV('BL10C:scaler1_calc4.VAL') self.trans = PV('BL10C:scaler1_calc8.VAL') self.motorPos = PV('mobiis:m2.RBV') self.scanStart = PV('lww:scan2.EXSC.VAL', callback=self.scanStartCALLBACK) while self.motorPos.get() is None: print self.motorPos.get() self.countDonePV = PV('BL10C:scaler1.CNT', callback=self.countDoneCALLBACK) """ self.time0 = time.time() self.datrange = None #----------- end of add by LWW ----------------------- #wx.EVT_TIMER(self, ID_TIMER, self.onTimer) #self.timer = wx.Timer(self, ID_TIMER) """ self.Refresh() """ # --------------epics callback method--------------------- def scanStartCALLBACK(self, **kwargs): if kwargs['value'] is 1: # 0:Done, 1: scanning self.showPlotFrame(do_raise=True, clear=False) print 'New Scan Started!!!' else: print 'scan stopped!!!' return def countDoneCALLBACK(self, **kwargs): """ if kwargs['value'] is 1: # 0:Done, 1:Counting return ## self._flag = True self.plotYdata.append(self.trans.get()) self.plotXdata.append(self.motorPos.get()) ## self._flag = False """ self.plotXdata = kwargs['plotXdata'] self.plotYdata = kwargs['plotYdata'] print 'X:%s, Y:%s' % (len(self.plotXdata), len(self.plotYdata)) # Todo: we need new scan start sequence. # new list, graph clear, data save..., # print 'timer ', self.count, time.time() ## self.count += 1 #self.showPlotFrame(do_raise=False, clear=False) ## n = self.count if len(self.plotXdata) < 2: return # Todo: implementation of scan finish sequence. ''' if n >= self.npts: self.timer.Stop() self.timer_results() elif n <= 3: ''' if len(self.plotXdata) <= 3: self.plotframe.plot(self.plotXdata, self.plotYdata, linewidth=0.5, marker='o', markersize=6, xlabel='energy[eV]', ylabel='[count]') # , grid=False) else: xx = np.array(self.plotXdata) yy = np.array(self.plotYdata) self.plotframe.update_line(0, xx, yy, update_limits=len(xx) < 10, draw=True) etime = time.time() - self.time0 s = " %i / %i points in %8.4f s" % (len(self.plotXdata), len(self.plotYdata), etime) self.plotframe.write_message(s) if self.datrange is None: self.datrange = [min(self.plotXdata), max(self.plotXdata), min(self.plotYdata), max(self.plotYdata)] dr = [min(self.plotXdata), max(self.plotXdata), min(self.plotYdata), max(self.plotYdata)] lims = self.plotframe.panel.get_viewlimits() if dr[0] < lims[0] or dr[1] > lims[1] or dr[2] < lims[2] or dr[3] > lims[3]: self.datrange = dr ## if len(self.plotXdata) < len(self.x): ## nmax = min(int(n*1.6), len(self.x)-1) ## self.datrange[1] = self.x[nmax] self.plotframe.panel.set_xylims(self.datrange) def showPlotFrame(self, do_raise=True, clear=True): """make sure plot frame is enabled, and visible""" if self.plotframe is None: self.plotframe = PlotFrame(self, axissize=[0.08, 0.06, 0.91, 0.92]) # self.has_plot = False try: self.plotframe.Show() except wx.PyDeadObjectError: self.plotframe = PlotFrame(self, axissize=[0.08, 0.06, 0.91, 0.92]) self.plotframe.Show() if do_raise: self.plotframe.Raise() ''' if clear: self.plotframe.panel.clear() self.plotframe.reset_config() self.plotYdata = [] self.plotXdata = [] ''' if clear is True: self.clearPlot() def clearPlot(self): self.plotframe.panel.clear() self.plotframe.reset_config() self.plotYdata = [] self.plotXdata = [] ''' def onStartTimer(self,event=None): self.count = 0 self.up_count = 0 self.n_update = 1 self.datrange = None self.time0 = time.time() ### LWW self.start_mem= self.report_memory() self.timer.Start(10) # self.timer.Start(500) def timer_results(self): if (self.count < 2): return etime = time.time() - self.time0 tpp = etime/max(1,self.count) s = "drew %i points in %8.3f s: time/point= %8.4f s" % (self.count,etime,tpp) self.plotframe.write_message(s) self.time0 = 0 self.count = 0 self.datrange = None ''' ''' def onTimer(self, event): # print 'timer ', self.count, time.time() self.count += 1 self.showPlotFrame(do_raise=False, clear=False) n = self.count if n < 2: return if n >= self.npts: self.timer.Stop() self.timer_results() elif n <= 3: self.plotframe.plot(self.x[:n], self.y1[:n])# , grid=False) else: self.plotframe.update_line(0, self.x[:n], self.y1[:n], update_limits=n<10, draw=True) etime = time.time() - self.time0 s = " %i / %i points in %8.4f s" % (n,self.npts,etime) self.plotframe.write_message(s) if self.datrange is None: self.datrange = [min(self.x[:n]), max(self.x[:n]), min(self.y1[:n]),max(self.y1[:n])] dr = [min(self.x[:n]), max(self.x[:n]), min(self.y1[:n]), max(self.y1[:n])] lims = self.plotframe.panel.get_viewlimits() if dr[0] < lims[0] or dr[1] > lims[1] or dr[2] < lims[2] or dr[3] > lims[3]: self.datrange = dr if n < len(self.x): nmax = min(int(n*1.6), len(self.x)-1) self.datrange[1] = self.x[nmax] self.plotframe.panel.set_xylims(self.datrange) ''' def OnAbout(self, event): dlg = wx.MessageDialog(self, "This program shows MCA Spectra\n" "message dialog.", "About MPlot test", wx.OK | wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy() def OnExit(self, event): """ # 1st. disconnect PV(s) self.countDonePV.clear_callbacks() self.i0.disconnect() self.it.disconnect() self.iF.disconnect() self.trans.disconnect() self.motorPos.disconnect() """ try: if self.plotframe is not None: self.plotframe.onExit() except: pass self.Destroy()
class TestFrame(wx.Frame): def __init__(self, parent=None, *args,**kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE|wx.RESIZE_BORDER|wx.TAB_TRAVERSAL wx.Frame.__init__(self, parent, -1, '', wx.DefaultPosition, wx.Size(-1,-1), **kwds) self.SetTitle(" WXMPlot Plotting Demo") self.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.BOLD,False)) menu = wx.Menu() menu_exit = menu.Append(-1, "E&xit", "Terminate the program") menuBar = wx.MenuBar() menuBar.Append(menu, "&File"); self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.OnExit, menu_exit) self.Bind(wx.EVT_CLOSE, self.OnExit) self.plotframe = None self.create_data() framesizer = wx.BoxSizer(wx.VERTICAL) panel = wx.Panel(self, -1, size=(-1, -1)) panelsizer = wx.BoxSizer(wx.VERTICAL) panelsizer.Add( wx.StaticText(panel, -1, 'wxmplot 2D PlotPanel examples '), 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT|wx.EXPAND, 10) b10 = wx.Button(panel, -1, 'Example #1', size=(-1,-1)) b20 = wx.Button(panel, -1, 'Example #2', size=(-1,-1)) b22 = wx.Button(panel, -1, 'Plot with 2 axes', size=(-1,-1)) b31 = wx.Button(panel, -1, 'Plot with Errorbars', size=(-1,-1)) b32 = wx.Button(panel, -1, 'SemiLog Plot', size=(-1,-1)) b40 = wx.Button(panel, -1, 'Start Timed Plot', size=(-1,-1)) b50 = wx.Button(panel, -1, 'Stop Timed Plot', size=(-1,-1)) b60 = wx.Button(panel, -1, 'Plot 500,000 points', size=(-1,-1)) bmany1 = wx.Button(panel, -1, 'Plot 20 traces (delay_draw=False)', size=(-1,-1)) bmany2 = wx.Button(panel, -1, 'Plot 20 traces (delay_draw=True)', size=(-1,-1)) bmany3 = wx.Button(panel, -1, 'Plot 20 traces (use plot_many())', size=(-1,-1)) b10.Bind(wx.EVT_BUTTON,self.onPlot1) b20.Bind(wx.EVT_BUTTON,self.onPlot2) b22.Bind(wx.EVT_BUTTON,self.onPlot2Axes) b31.Bind(wx.EVT_BUTTON,self.onPlotErr) b32.Bind(wx.EVT_BUTTON,self.onPlotSLog) b40.Bind(wx.EVT_BUTTON,self.onStartTimer) b50.Bind(wx.EVT_BUTTON,self.onStopTimer) b60.Bind(wx.EVT_BUTTON,self.onPlotBig) bmany1.Bind(wx.EVT_BUTTON,self.onPlotMany_Slow) bmany2.Bind(wx.EVT_BUTTON,self.onPlotMany_Delay) bmany3.Bind(wx.EVT_BUTTON,self.onPlotMany_Fast) panelsizer.Add(b10, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b20, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b22, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b31, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b32, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b40, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b50, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(b60, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(bmany1, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(bmany2, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panelsizer.Add(bmany3, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.LEFT, 5) panel.SetSizer(panelsizer) panelsizer.Fit(panel) framesizer.Add(panel, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER|wx.EXPAND,2) self.SetSizer(framesizer) framesizer.Fit(self) self.Bind(wx.EVT_TIMER, self.onTimer) self.timer = wx.Timer(self) self.Refresh() def create_data(self): self.count = 0 self.x = x = arange(0.0,25.0,0.1) self.y1 = 4*cos(2*pi*(x-1)/5.7)/(6+x) + 2*sin(2*pi*(x-1)/2.2)/(10) self.y2 = sin(2*pi*x/30.0) self.y3 = -pi + 2*(x/10. + exp(-(x-3)/5.0)) self.y4 = exp(0.01 + 0.5*x ) / (x+2) self.y5 = 3000 * self.y3 self.npts = len(self.x) self.bigx = linspace(0, 2500, 500000) self.bigy = (sin(pi*self.bigx/140.0) + cos(pi*self.bigx/277.0) + cos(pi*self.bigx/820.0)) self.many_dlist = [(self.x, self.y1)] for i in range(19): self.many_dlist.append((self.x, sin(2*(i+1)*x/23.0))) def ShowPlotFrame(self, do_raise=True, clear=True): "make sure plot frame is enabled, and visible" if self.plotframe is None: self.plotframe = PlotFrame(self) self.has_plot = False try: self.plotframe.Show() except PyDeadObjectError: self.plotframe = PlotFrame(self) self.plotframe.Show() if do_raise: self.plotframe.Raise() if clear: self.plotframe.panel.clear() self.plotframe.reset_config() def onPlot1(self,event=None): self.ShowPlotFrame() self.plotframe.plot(self.x,self.y1) self.plotframe.oplot(self.x,self.y2) self.plotframe.write_message("Plot 1") def onPlot2(self,event=None): self.ShowPlotFrame() x = arange(100) y1 = cos(pi*x/72) y2 = sin(pi*x/23) self.plotframe.plot(x, y1, color='red') self.plotframe.oplot(x, y2, color='green3', marker='+') self.plotframe.write_message("Plot 2") def onPlotErr(self,event=None): self.ShowPlotFrame() npts = 81 x = linspace(0, 40.0, npts) y = 0.4 * cos(x/2.0) + random.normal(scale=0.03, size=npts) dy = 0.03 * (ones(npts) + random.normal(scale=0.2, size=npts)) self.plotframe.plot(x, y, dy=dy, color='red', linewidth=0, xlabel='x', ylabel='y', marker='o', title='Plot with error bars') self.plotframe.write_message("Errorbars!") def onPlot2Axes(self,event=None): self.ShowPlotFrame() self.plotframe.plot( self.x,self.y2, color='black',style='dashed') self.plotframe.oplot(self.x,self.y5, color='red', side='right') self.plotframe.write_message("Plot with 2 axes") def onPlotSLog(self,event=None): self.ShowPlotFrame() self.plotframe.plot( self.x,self.y4,ylog_scale=True, color='black',style='dashed') self.plotframe.write_message("Semi Log Plot") def onPlotBig(self,event=None): self.ShowPlotFrame() t0 = time.time() self.plotframe.plot(self.bigx, self.bigy, marker='+', linewidth=0) dt = time.time()-t0 self.plotframe.write_message( "Plot array with npts=%i, elapsed time=%8.3f s" % (len(self.bigx),dt)) def onPlotMany_Slow(self, event=None): self.ShowPlotFrame() dlist = self.many_dlist t0 = time.time() opts = dict(title='Plot 20 traces without delay_draw', show_legend=True, xlabel='x') self.plotframe.plot(dlist[0][0], dlist[0][1], **opts) for tdat in dlist[1:]: self.plotframe.oplot(tdat[0], tdat[1]) dt = time.time()-t0 self.plotframe.write_message( "Plot 20 traces without delay_draw=True, elapsed time=%8.3f s" % (dt)) def onPlotMany_Delay(self, event=None): self.ShowPlotFrame() dlist = self.many_dlist t0 = time.time() opts = dict(title='Plot 20 traces with delay_draw', show_legend=True, xlabel='x') self.plotframe.plot(dlist[0][0], dlist[0][1], delay_draw=True, **opts) for tdat in dlist[1:-1]: self.plotframe.oplot(tdat[0], tdat[1], delay_draw=True) self.plotframe.oplot(dlist[-1][0], dlist[-1][1]) dt = time.time()-t0 self.plotframe.write_message( "Plot 20 traces with delay_draw=True, elapsed time=%8.3f s" % (dt)) def onPlotMany_Fast(self, event=None): self.ShowPlotFrame() dlist = self.many_dlist t0 = time.time() opts = dict(title='Plot 20 traces using plot_many()', show_legend=True, xlabel='x') self.plotframe.plot_many(dlist, **opts) dt = time.time()-t0 self.plotframe.write_message( "Plot 20 traces with plot_many(), elapsed time=%8.3f s" % (dt)) def report_memory(i): pid = os.getpid() if os.name == 'posix': mem = os.popen("ps -o rss -p %i" % pid).readlines()[1].split()[0] else: mem = 0 return int(mem) def onStartTimer(self,event=None): self.count = 0 self.up_count = 0 self.n_update = 1 self.datrange = None self.time0 = time.time() self.start_mem= self.report_memory() self.timer.Start(10) def timer_results(self): if (self.count < 2): return etime = time.time() - self.time0 tpp = etime/max(1,self.count) s = "drew %i points in %8.3f s: time/point= %8.4f s" % (self.count,etime,tpp) self.plotframe.write_message(s) self.time0 = 0 self.count = 0 self.datrange = None def onStopTimer(self,event=None): self.timer.Stop() try: self.timer_results() except: pass def onTimer(self, event): # print 'timer ', self.count, time.time() self.count += 1 n = self.count if n < 2: self.ShowPlotFrame(do_raise=False, clear=False) return if n >= self.npts: self.timer.Stop() self.timer_results() elif n <= 3: self.plotframe.plot(self.x[:n], self.y1[:n])# , grid=False) else: self.plotframe.update_line(0, self.x[:n], self.y1[:n], update_limits=True, draw=True) etime = time.time() - self.time0 s = " %i / %i points in %8.4f s" % (n,self.npts,etime) self.plotframe.write_message(s) def OnAbout(self, event): dlg = wx.MessageDialog(self, "This sample program shows some\n" "examples of WXMPlot PlotFrame.\n" "message dialog.", "About WXMPlot test", wx.OK | wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy() def OnExit(self, event): try: if self.plotframe != None: self.plotframe.onExit() except: pass self.Destroy()