Python in real time

I get some data in two arrays: one for time and one for value. When I reach 1000 points, I start the signal and build these points (x = time, y = value).

I need to keep the previous plots in one drawing, but only a reasonable number to avoid slowing down the process. For example, I would like to save 10,000 points on my graph. The matplotlib interactive plot works fine, but I don’t know how to erase the first glasses, and it slows down my computer very quickly. I looked into matplotlib.animation, but it seems to be repeating the same plot and not updating it.

I am really looking for an easy solution to avoid slowdown.

As I get for a very large amount of time, I delete the input in each loop (the 1001st point is stored in the 1st line, etc.).

Here is what I have at the moment, but it saves all the points on the chart:

import matplotlib.pyplot as plt

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

def continuous_plot(x, fx, x2, fx2):
  plt.plot(x, fx, 'bo', markersize=1)
  plt.plot(x2, fx2, 'ro', markersize=1)
  plt.draw()

I call the init function once, and the continuum_plot is in a process called every time I have 1000 points in my array.

+4
source share
3 answers

The easiest solution you have is to replace the X and Y values ​​of the existing graph. (Or just the value Y, if your X data is not changing. 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
ax.relim() 
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.show(block=False)

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

        # set the new data
        li.set_ydata(y)

        fig.canvas.draw()

        time.sleep(0.01)
    except KeyboardInterrupt:
        break

. 100 ( time.sleep), 70-80, , 4 . YMMV ..

+6

, matplot.

 import collections
 array = collections.deque([None] * 1000, maxlen=1000)

, .

+5

, , : bt "". line.set_data() canvas.draw() . ( ). , , , . , . . http://www.github.com/ceyzeriat/joystick/ https://pypi.python.org/pypi/joystick ( pip install )

:

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
        self.xdata = jk.core.add_datapoint(self.xdata, time.time(), xnptsmax=self.mygraph.xnptsmax)
        # concatenate data on the fake data y-axis
        self.ydata = jk.core.add_datapoint(self.ydata, np.random.random(), xnptsmax=self.mygraph.xnptsmax)
        self.mygraph.set_xydata(t, self.ydata)

t = test()
t.start()
t.stop()
0

Source: https://habr.com/ru/post/1548652/


All Articles