简体   繁体   English

使用 Python3 实时绘制多个图

[英]Multiple graphs in real time with Python3

I am new to python3 and my task is to receive multiple signals from different sensors and graph them.我是 python3 的新手,我的任务是接收来自不同传感器的多个信号并绘制它们。 I already managed to graph the signals in real-time and also use the subplot (), subplots () functions but the problem is that all the signals come out on a single graph and I need each signal to have its own graph.我已经设法实时绘制信号并且还使用了 subplot ()、subplots () 函数,但问题是所有信号都出现在一个图表上,我需要每个信号都有自己的图表。 ps: I've already used threads for each signal but the subsequent threads kill the process of the previous thread. ps:我已经为每个信号使用了线程,但是后续线程杀死了前一个线程的进程。

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

def animate(num,hl,listax,listay,hl2=None,listax2=None,listay2=None):
    plt.xlim(listax[0],listax[-1])
    xs = np.array(listax)
    ys = np.array(listay)
    hl.set_data(xs,ys)
    if hl2 != None:
        #plt.xlim(listax[0],listax[-1])
        xs2 = np.array(listax2)
        ys2 = np.array(listay2)
        hl2.set_data(xs2,ys2)
        return hl , hl2
    else:   
        return hl,

def main(time,signal,signal2):

     fig , (hl,hl2)= plt.subplots(2,1)
     hl, = plt.plot(time,signal)
     hl2, = plt.plot(time,signal2)
     ani = animation.FuncAnimation(fig,animate,fargs=(hl,time,signal,hl2,time,signal2),interval=1,blit=False)
     plt.show()

main(time,signal,signal2)
#time is the list of  time when the measures were taken
#Signal,signal2 are lists of the measures taken on those times
#those lists are in constant upgrade mode trough a thread that upgrades those lists

Made a sample according to doc and your code.根据文档和您的代码制作了一个示例。 Note frame is used for animation one-shot, but in code frame is defined as list (range(len(time)). If you want to set axis limit initially, use init parameter of FuncAnimation. (In case you are using Jupyter... on Jupyter animation don't wokk, please call py from terminal etc..)注意frame用于 animation one-shot,但在代码中,frame 定义为 list (range(len(time))。如果要初始设置轴限制,请使用 FuncAnimation 的 init 参数。(如果您使用的是 Jupyter。 .. 在 Jupyter animation 上不要炒锅,请从终端等调用 py。)

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


def update(frame):
    xdata.append(time[frame])
    ydata.append(signal[frame])
    ydata2.append(signal2[frame])
    ln.set_data(xdata, ydata)
    ln2.set_data(xdata, ydata2)
    return ln, ln2


time = range(0, 10)
signal = range(10, 20)
signal2 = range(50, 60)

frame = range(len(time))

fig = plt.figure()

ax = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax.set_xlim(0, 100)
ax2.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax2.set_ylim(0, 100)

ln, = ax.plot([], [], "o")
ln2, = ax2.plot([], [], "ro")

xdata = []
ydata = []
ydata2 = []

ani = animation.FuncAnimation(fig, update, frames=frame, blit=True)

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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