I am trying to plot data to the same figure every 10 seconds, but after every iteration a new figure gets displayed.
How can I update the plot in the same figure?
Is this the most efficient way to do so? I need to handle far more data in the future and need to update the plots in the fastest possible way.
Here is the code:
plotToFigure()
is receiving 8.000.000 numerical values every 10 sec from data_handler
, so np_data_list
is an array containing these 8.000.000 values. It then plots the data to a figure (starting from 1000th value every 1000th value):
def plotToFigure(self):
#
np_data_list = self.data_handler.np_data_list[0]
x_axis = np.arange(len(np_data_list))
#
self.line, = self.axis.plot(x_axis[1000::1000], np_data_list[1000::1000])
#
self.canvas = self.figure.canvas
self.canvas.draw() # Do I need this?
self.canvas.flush_events()
self.background = self.canvas.copy_from_bbox(self.axis.bbox)
#
self.figure.show()
#plt.show() # What is the difference?
#
updateFigure()
is receiving again 8.000.000 and should update the figure with the new values (the second updateFigure()
function is basically doing the same as the first one, but also not updating the current figure);
If self.figure.show()
is not in the code, only the figure from plotToFigure()
will get displayed and the figure will not get updated:
def updateFigure(self):
#
np_data_list = self.data_handler.np_data_list[0]
self.line.set_ydata(np_data_list[1000::1000])
#
self.canvas.restore_region(self.background) # restore background
self.axis.draw_artist(self.line) # redraw just the line
self.figure.canvas.blit(self.axis.bbox) # fill in the axes rectangle
#
self.figure.show()
#
self.background = self.canvas.copy_from_bbox(self.axis.bbox)
#
'''
def updateFigure(self):
#
np_data_list = self.data_handler.np_data_list[0]
self.line.set_ydata(np_data_list[1000::1000])
#
plt.draw()
plt.pause(0.1)
#
#self.canvas.draw() ??
#self.canvas.flush_events() ??
#
'''
This is an example where data_handler is receiving 8.000.000 numerical values, every 10 seconds, three times in a row and passes it to plotter:
# ------ MAIN PROGRAM ------ #
...
...
#
fig, ax = plt.subplots()
data_plotter = class_DataPlotter.DataPlotter(data_handler, fig, ax)
#
data_handler.getDataFromDataAcquisitionObject()
data_plotter.plotToFigure()
#
data_handler.getDataFromDataAcquisitionObject()
data_plotter.updateFigure()
#
data_handler.getDataFromDataAcquisitionObject()
data_plotter.updateFigure()
#
...
...
#
This is the output I get in Pycharm