# Dynamically updating plot in matplotlib

Each Answer to this Q is separated by one/two green lines.

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. The time of arrival for the data is uncertain. I want the plot to be updated when data is received. I searched on how to do this and found two methods:

1. Clear the plot and re-draw the plot with all the points again.
2. Animate the plot by changing it after a particular interval.

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.
The second one is also not preferable as time of arrival of data is uncertain and I want the plot to update only when the data is received.

Is there a way in which I can update the plot just by adding more points to it only when the data is received?

Is there a way in which I can update the plot just by adding more point[s] to it…

There are a number of ways of animating data in matplotlib, depending on the version you have. Have you seen the matplotlib cookbook examples? Also, check out the more modern animation examples in the matplotlib documentation. Finally, the animation API defines a function FuncAnimation which animates a function in time. This function could just be the function you use to acquire your data.

Each method basically sets the `data` property of the object being drawn, so doesn’t require clearing the screen or figure. The `data` 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).

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()
``````

Then when you receive data from the serial port just call `update_line`.

In order to do this without FuncAnimation (eg you want to execute other parts of the code while the plot is being produced or you want to be updating several plots at the same time), calling `draw` alone does not produce the plot (at least with the qt backend).

The following works for me:

``````import matplotlib.pyplot as plt
plt.ion()
class DynamicUpdate():
#Suppose we know the x range
min_x = 0
max_x = 10

def on_launch(self):
#Set up plot
self.figure, self.ax = plt.subplots()
self.lines, = self.ax.plot([],[], 'o')
#Autoscale on unknown axis and known lims on the other
self.ax.set_autoscaley_on(True)
self.ax.set_xlim(self.min_x, self.max_x)
#Other stuff
self.ax.grid()
...

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
self.ax.relim()
self.ax.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 = DynamicUpdate()
d()
``````

Here is a way which allows to remove points after a certain number of points plotted:

``````import matplotlib.pyplot as plt
# generate axes object
ax = plt.axes()

# set limits
plt.xlim(0,10)
plt.ylim(0,10)

for i in range(10):
ax.scatter([i], [i])
ax.plot([i], [i+1], 'rx')

# draw the plot
plt.draw()
plt.pause(0.01) #is necessary for the plot to update for some reason

# start removing points if you don't want all shown
if i>2:
ax.lines.remove()
ax.collections.remove()
``````

I know I’m late to answer this question, but for your issue you could look into the “joystick” package. I designed it for plotting a stream of data from the serial port, but it works for any stream. It also allows for interactive text logging or image plotting (in addition to graph plotting).
No need to do your own loops in a separate thread, the package takes care of it, just give the update frequency you wish. Plus the terminal remains available for monitoring commands while plotting.
See http://www.github.com/ceyzeriat/joystick/ or https://pypi.python.org/pypi/joystick (use pip install joystick to install)

Just replace np.random.random() by your real data point read from the serial port in the code below:

``````import joystick as jk
import numpy as np
import time

class test(jk.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.Graph(name="test", size=(500, 500), pos=(50, 50), fmt="go-", xnpts=10000, xnptsmax=10000, xylim=(None, None, 0, 1)))

@_infinite_loop(wait_time=0.2)
def _generate_data(self):  # function looped every 0.2 second to read or produce data
"""
Loop starting with the simulation start, getting data and
pushing it to the graph every 0.2 seconds
"""
# concatenate data on the time x-axis
{"ft_lang_label":"__label__en","ft_lang_prob":0.7577753,"math_prob":0.9109933,"size":5669,"snap":"2022-27-2022-33","text_gpt3_token_len":1429,"char_repetition_ratio":0.113857016,"word_repetition_ratio":0.037691403,"special_character_ratio":0.25542423,"punctuation_ratio":0.16736753,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99248475,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-05T12:41:03Z\",\"WARC-Record-ID\":\"<urn:uuid:0391514b-4d88-419a-8dd4-549c216ce9ab>\",\"Content-Length\":\"83478\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ec0a14bd-c540-47df-9a2a-5570d508a224>\",\"WARC-Concurrent-To\":\"<urn:uuid:a71e8178-689c-4994-ae05-7ebf8d559b64>\",\"WARC-IP-Address\":\"172.67.205.64\",\"WARC-Target-URI\":\"https://techstalking.com/dynamically-updating-plot-in-matplotlib/\",\"WARC-Payload-Digest\":\"sha1:KSMGGAS2B7HD3A3GA7JCG6JMAGXIEFPF\",\"WARC-Block-Digest\":\"sha1:D6CNQN26V3YCFKPB6RCX3MWU5Y3P7OGP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104576719.83_warc_CC-MAIN-20220705113756-20220705143756-00209.warc.gz\"}"}