HarryPotfleur HarryPotfleur - 3 months ago 36x
Python Question

python real time plotting

I acquire some data in 2 array : 1 for the time, and one for the value. When I reach 1000 points, I trigger a signal and plot these points (x=time, y=value).
I need to keep on the same figure the previous plots, but only a reasonable number to avoid slowing down the process. For example I would like to keep 10 000 points on my graph.
The matplotlib interractive plot works fine but I don't know how to erase the first points and it slows my computer very quickly.
I looked into matplotlib.animation but it only seems to repeat the same plot, and not really actualise it.

I'm really looking for a light solution, to avoid any slowing.

EDIT: As I acquire for a very large amount of time, I erase the input data on every loop (the 1001st point is stored in the 1st row and so on)

EDIT 2: Here is what I have for now, but it keeps all the points on the graph:

import matplotlib.pyplot as plt

def init_plot():
plt.title("Test d\'acqusition",fontsize=20)
plt.xlabel("Temps(s)", fontsize=20)
plt.ylabel("Tension (V)", fontsize=20)

def continuous_plot(x,fx,x2,fx2):

I call the init function once, and the continous_plot is in a process, called everytime I have 1000 points in my array


The lightest solution you may have is to replace the X and Y values of an existing plot. (Or the Y value only, if your X data does not change. A simple example:

import matplotlib.pyplot as plt
import numpy as np
import time

fig = plt.figure()
ax = fig.add_subplot(111)

# some X and Y data
x = np.arange(10000)
y = np.random.randn(10000)

li, = ax.plot(x, y)

# draw and show it

# loop to update the data
while True:
        y[:-10] = y[10:]
        y[-10:] = np.random.randn(10)

        # set the new data


    except KeyboardInterrupt:

This solution is quite fast, as well. The maximum speed of the above code is 100 redraws per second (limited by the time.sleep), I get around 70-80, which means that one redraw takes around 4 ms. But YMMV depending on the backend, etc.