简体   繁体   中英

How to use NumPy arrays while Plotting Live Data in Real-Time?

How to use numpy arrays correctly during live construction plot, I mean if I use these arrays i push values into and everything is good with the exception of line which starts from (0;0) and points each time on current plot's point, in other words first end stays stable, but second changes it location. What to do with this? Also I don't understand how to pass parameters by reference at animation().

s = '.........................................................................................'+
string.ascii_uppercase
letters = [s[randint(0, len(s)-1)] for _ in range(5000)] #массив букв
time    = [i for i in range(5000)]
example = [randint(0, 100) for _ in range(5000)]

t = count()
frequency = 0
time_x = np.zeros(len(time))
key_y = np.zeros(len(time))

cnt = 0

def animate(i):
    global cnt
    time_x[cnt] = time[cnt]
    key_y[cnt] = example[cnt]

    print (time_x[cnt], key_y[cnt])
    plt.cla()
    plt.plot(time_x, key_y)
    cnt += 1

ani = FuncAnimation(plt.gcf(), animate, interval=3000)

plt.show()

In the line where you do plt.plot(time_x, key_y) , because time_x and key_y are instantiated as an array of zeros, there was always lines being drawn from your last generated point to a [0, 0] location (until you generate the points where cnt=4999 ).

eg If cnt=2 in your data generation loop, it might look like:

time_x = [  0.0, 1.0,  2.0, 0.0, 0.0, 0.0, ...]
key_y  = [100.0, 1.0, 52.0, 0.0, 0.0, 0.0, ...]

which means you're also plotting a lot of zeros!

I think you only wanted to plot up to cnt (for which you had generated data), ie plt.plot(time_x[:cnt], key_y[:cnt]) , or in your example:

import numpy as np
from random import randint
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

s = '.........................................................................................'
letters = [s[randint(0, len(s)-1)] for _ in range(5000)]
time = [i for i in range(5000)]
example = [randint(0, 100) for _ in range(5000)]

time_x = np.zeros(len(time))
key_y = np.zeros(len(time))

def animate(cnt, param_1, param_2):
    time_x[cnt] = time[cnt]
    key_y[cnt] = example[cnt]
    print(cnt, time_x[cnt], key_y[cnt], f'Extra params: {param_1}, {param_2}')
    plt.cla()
    plt.plot(time_x[:cnt], key_y[:cnt])


extra_params = ('p_1', 'p_2')

ani = FuncAnimation(plt.gcf(), animate, interval=3000, fargs=extra_params)
plt.show()

The first parameter passed to animate ( i ) is already what the global cnt used to do in your original code, so you can just replace it. If you want to pass in specific cnt values, as suggested in the docs , you can try adding a frames argument.

If you want to pass in other parameters, you can use the fargs argument. You can pass in extra data as a tuple, as shown by extra_params .

In the event you want to do something more complicated, I would use a python class to store the state instead, as shown in this SO post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM