0

Here I have plotted a line chart with two lists

import random as random
import matplotlib.pyplot as plt
lis1=random.sample(range(1, 100), 10)
lis2 = random.sample(range(1, 100), 10)
plt.plot(range(0,len(lis1), 1), lis1)
plt.plot(range(0,len(lis2), 1), lis2)
plt.show()

Now, I'm getting a third list from Arduino in realtime. My question is how to plot that third list/line over this plot without redrawing entire chart.

EDIT: Third list is something like this

import time
lis3 =[]
for i in range(10):
    lis3.append(i)
    time.sleep(1)
ramesh
  • 1,187
  • 7
  • 19
  • 42

2 Answers2

0

have a look at the following post: When to use cla(), clf() or close() for clearing a plot in matplotlib?

i think you can clear the figure by using plt.clear() in an timer event. Re-drawing can be done by using the plt.draw() function. Because of the realtime data you have to have a function which is called after a certain delay. There i would call this plt.clear() or plt.draw() function. Afterwards you have to re fill the lists or make a new list to draw the third line.

I don't know a better solution and maybe that's not what you want, because it's some keind of re-drawing but i hope that this is useful for you!

Also have a look at: Dynamically updating plot in matplotlib

How to update a plot in matplotlib?

Community
  • 1
  • 1
0

plt.show() will display the current chart that you're working on whereas plt.draw() will re-draw the figure. This essentially allows you to change the graph as your data changes

The plt.draw docs state:

This is used in interactive mode to update a figure that has been altered using one or more plot object method calls; it is not needed if figure modification is done entirely with pyplot functions, if a sequence of modifications ends with a pyplot function, or if matplotlib is in non-interactive mode and the sequence of modifications ends with show() or savefig().

mmenschig
  • 1,088
  • 14
  • 22