diff --git a/pyrelacs/ui/plots/continously.py b/pyrelacs/ui/plots/continously.py index 1b7fafb..297591d 100644 --- a/pyrelacs/ui/plots/continously.py +++ b/pyrelacs/ui/plots/continously.py @@ -15,6 +15,7 @@ class Continously: def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer): self.figure = figure self.buffer = buffer + self.last_plotted_index = 0 def plot(self, *args, **kwargs): self.figure.setBackground("w") @@ -38,7 +39,6 @@ class Continously: self.timer = QTimer() self.CHUNK_PLOT = int(self.buffer.samplerate / 6) self.PLOT_HISTORY = 500_000 # The amount of data you want to keep on screen - self.last_plotted_index = 0 self.timer.setInterval(150) self.timer.timeout.connect(self.update_plot) self.timer.start() @@ -49,18 +49,20 @@ class Continously: start_time = time.time() if total_count - self.last_plotted_index >= self.CHUNK_PLOT: - times, items = self.buffer.read( - self.last_plotted_index, - extend=self.CHUNK_PLOT, - ) - - self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :] - self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :] - self.line.setData( - self.time, - self.data, - ) - self.last_plotted_index += self.CHUNK_PLOT + try: + times, items = self.buffer.read( + self.last_plotted_index, + extend=self.CHUNK_PLOT, + ) + self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :] + self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :] + self.line.setData( + self.time, + self.data, + ) + self.last_plotted_index += self.CHUNK_PLOT + except IndexError: + log.error("Could not acces the data from the buffer for plotting") end_time = time.time() log.debug(f"total time for plotting {end_time - start_time}") else: @@ -68,6 +70,18 @@ class Continously: def stop_plotting(self): self.timer.stop() + total_count = self.buffer.totalcount() + times, items = self.buffer.read( + self.last_plotted_index, + extend=total_count - self.last_plotted_index, + ) + self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :] + self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :] + self.line.setData( + self.time, + self.data, + ) + self.last_plotted_index += total_count - self.last_plotted_index def refresh(self): self.continous_ax.enableAutoRange()