def update_graph(self, data_points): """This version takes care of time axis, you only pass values""" if self._data == {}: self.release_pen(-1) self.__start_time = pg.time() stamp = pg.time() - self.__start_time self.plot.setXRange(stamp - self.window_duration_s, stamp) for label, value in data_points.items(): if label not in self._data: pen_index = self.pick_pen() curve = self.plot.plot([],[], pen=self._pens[pen_index], name=label) self._data[label] = {'curve': curve, 'pen_index': pen_index, 'time': deque(), 'values': deque()} self._data[label]['time'].append(stamp) self._data[label]['values'].append(value) empty = [] for label, dat in self._data.items(): while dat['time'] and dat['time'][0] < stamp - self._buffer_duration_s: dat['time'].popleft() dat['values'].popleft() if not dat['time']: empty.append(label) dat['curve'].setData(dat['time'], dat['values']) for label in empty: self.release_pen(self._data[label]['pen_index']) del self._data[label]
def _draw_process_func(q: multiprocessing.Queue): try: app = QtGui.QApplication(["Robot live plotting"]) win = pg.GraphicsWindow(title="Basic plotting examples") win.resize(1000, 1000) win.setWindowTitle("Robot plotting") # Enable antialiasing for prettier plots pg.setConfigOptions(antialias=False) # scatter_item = pg.ScatterPlotItem(x =1,2,3) p1 = win.addPlot( title="Parametric Plot", symbolPen="w", autoDownsample=True ) curve = p1.plot() lastTime = time() samples = None while True: new_sample = q.get() if new_sample is None: # sentinel object read win.close() return 0 if samples is None: samples = np.array(new_sample) else: samples = np.vstack([samples, new_sample]) curve.setData(samples, pen="w") # performance metrics fps = None now = time() dt = now - lastTime lastTime = now if fps is None: fps = 1.0 / dt else: s = np.clip(dt * 3.0, 0, 1) fps = fps * (1 - s) + (1.0 / dt) * s fps_str = f"fps: {int(fps)}, samples : {len(samples)}" p1.setTitle(fps_str) app.processEvents() except Exception as e: print(f"An exception was raised in the drawing thread: {e}")
def _draw_process_func(q: multiprocessing.Queue): app = QtGui.QApplication(["Robot live plotting"]) win = pg.GraphicsWindow(title="Basic plotting examples") win.resize(1000, 1000) win.setWindowTitle('Robot plotting') # Enable antialiasing for prettier plots pg.setConfigOptions(antialias=False) # scatter_item = pg.ScatterPlotItem(x =1,2,3) p1 = win.addPlot(title="Parametric Plot", symbolPen='w', autoDownsample=True) curve = p1.plot() lastTime = time() samples = None samples = q.get() while(True): new_sample = q.get() if(new_sample is None): return 0 samples = np.vstack([samples, new_sample]) #curve.setData(samples, pen='w',graph="width",symbolPen=(255,255,255)) curve.setData(samples, pen='w') # performance metrics fps = None now = time() dt = now - lastTime lastTime = now if fps is None: fps = 1.0/dt else: s = np.clip(dt*3., 0, 1) fps = fps * (1-s) + (1.0/dt) * s fps_str = f'fps: {int(fps)}, samples : {len(samples)}' #p1.setTitle('%0.2f fps' % fps) p1.setTitle(fps_str) app.processEvents()
def update(): t = pg.time() data = np.ones(100) * np.sin(t) data[50:60] += np.sin(t) global curve curve.setData(data)
def update(): t=pg.time() for im, phase in zip(ims, (0, np.pi/2)): # Create amplitude-modulated noisy Gaussian z=np.sin(t - phase)**2*z_0+np.random.random(z_0.shape)*0.1 levels=im.levels im.setImage(z,autoLevels=False)
def newData(self, data): # Get rid of old data minTime = None now = pg.time() while len(self.times) > 0 and self.times[0] < (now - self.timeSpin.value()): self.times.pop(0) self.values.pop(0) if len(self.times) > 0 and (minTime is None or self.times[0] < minTime): minTime = self.times[0] if minTime is None: minTime = data[0] # add new data draw = False if self.lastPlotTime is None or now - self.lastPlotTime > 0.05: draw = True self.lastPlotTime = now self.values.append(data[1]) self.times.append(data[0]) if draw: self.plotCurve.setData( np.array(self.times) - minTime, np.array(self.values))
def set_data(self,data): start=pg.time() self.data = data.reset_index(drop=True) self.low,self.high = (self.data['low'].values.min(),self.data['high'].values.max()) if len(data)>0 else (0,1) self.generatePicture() self.informViewBoundsChanged() # if not self.scene() is None: # self.scene().update() #強制圖形更新 end=pg.time() if len(self.timelist)<100: self.timelist.append((end-start)) else: self.timelist.pop(0) self.timelist.append((end-start)) if sum(self.timelist)!=0 and len(self.timelist)>0: ep=int(1/(sum(self.timelist)/len(self.timelist))) else: ep=0 print('每100張FPS: ',ep)
def run(self): while True: if self.stopped: break try: n = int(self.port.readline()) t = pg.time() except: continue self.sigNewData.emit((t, n))