I am making an application in Python which collects data from a serial port and plots a graph of the collected data against arrival time. I want the plot to be updated when data is received.I searched on how to do this and found two methods: I do not prefer the first one as the program runs and collects data for a long time (a day for example), and redrawing the plot will be pretty slow.def on_running(self, xdata, ydata): #Update data (with the new _and_ the old points) self.lines.set_xdata(xdata) self.lines.set_ydata(ydata) #Need both of these in order to rescale relim() autoscale_view() #We need to draw *and* flush self.figure.canvas.draw() self.figure.canvas.flush_events() #Example def __call__(self): import numpy as np import time self.on_launch() xdata =  ydata =  for x in np.arange(0,10,0.5): xdata.append(x) ydata.append(np.exp(-x**2) 10*np.exp(-(x-7)**2)) self.on_running(xdata, ydata) time.sleep(1) return xdata, ydata d = Dynamic Update() d() I know I'm late to answer this question, but for your issue you could look into the "joystick" package.
Given that you say that your data arrival time is uncertain your best bet is probably just to do something like: import matplotlib.pyplot as plt import numpy hl, = plt.plot(, ) def update_line(hl, new_data): hl.set_xdata(numpy.append(hl.get_xdata(), new_data)) hl.set_ydata(numpy.append(hl.get_ydata(), new_data)) plt.draw() Since there is no call to show(), the plot never appears on the screen.
If I call show(), it blocks and doesn't perform the updates. gist.github.com/daviddoria/027b5c158b6f200527a4import matplotlib.pyplot as plt plt.ion() class Dynamic Update(): #Suppose we know the x range min_x = 0 max_x = 10 def on_launch(self): #Set up plot self.figure, = plt.subplots() self.lines, = plot(,, 'o') #Autoscale on unknown axis and known lims on the other set_autoscaley_on(True) set_xlim(self.min_x, self.max_x) #Other stuff grid() ...
This function could just be the function you use to acquire your data.
Each method basically sets the property can simply be extended, so you can keep the previous points and just keep adding to your line (or image or whatever you are drawing).
Joystick): # initialize the infinite loop decorator _infinite_loop = jk.deco_infinite_loop() def _init(self, *args, **kwargs): """ Function called at initialization, see the doc """ self._t0 = time.time() # initialize time self.xdata = np.array([self._t0]) # time x-axis self.ydata = np.array([0.0]) # fake data y-axis # create a graph frame self.mygraph = self.add_frame(jk.