Matplotlib 用于实时打印的子地块的函数

Matplotlib 用于实时打印的子地块的函数,matplotlib,subplot,matplotlib-animation,Matplotlib,Subplot,Matplotlib Animation,我有一个代码,它为4个不同的传感器提供了4个单独的实时绘图图。但是,我希望他们在一个单一的框架使用子地块。粘贴两段正在绘制我的图形的代码。我应该如何修改它们,使我的输出成为4个活动图的子图 def makeFigure(xLimit, yLimit, title): xmin, xmax = xLimit ymin, ymax = yLimit fig = plt.figure() ax = plt.axes(xlim=(xmin, xmax), ylim=(in

我有一个代码,它为4个不同的传感器提供了4个单独的实时绘图图。但是,我希望他们在一个单一的框架使用子地块。粘贴两段正在绘制我的图形的代码。我应该如何修改它们,使我的输出成为4个活动图的子图

def makeFigure(xLimit, yLimit, title):
    xmin, xmax = xLimit
    ymin, ymax = yLimit
    fig = plt.figure()
    ax = plt.axes(xlim=(xmin, xmax), ylim=(int(ymin - (ymax - ymin) / 10), int(ymax + (ymax - ymin) / 10)))
    ax.set_title(title)
    ax.set_xlabel("Time")
    ax.set_ylabel("Detector Output")
    ax.grid(True)
    return fig, ax

我已经创建了四个子情节的动画,尽管它们并不满足您所寻找的所有图形要求。动画函数(i)调用绘图函数(k)。我对这类动画没有太多的经验,所以我希望它能对你的发展有所帮助

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

pltInterval = 50

t = np.arange(0.01, 5.0, 0.01)
ss1 = np.sin(2 * np.pi * t)
ss2 = np.sin(3 * np.pi * t)
ss3 = np.sin(4 * np.pi * t)
ss4 = np.sin(5 * np.pi * t)
yy = [ss1,ss2,ss3,ss4]
color = ['r', 'g', 'b', 'y'] 
title = ['Detector A', 'Detector B', 'Detector C', 'Detector D']

def example_plot(ax,i,y,k):
    ax.plot(t[:i], y[:i], color=color[k], linestyle='--')
    ax.set_xlabel('Time', fontsize=12)
    ax.set_ylabel('Detector Output', fontsize=12)
    ax.set_title(title[k], fontsize=14)
    
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)

def update(i):
    for k,(ax,y) in enumerate(zip(axs.flat,yy)):
        example_plot(ax,i,y,k)

anim = FuncAnimation(fig, update, frames=50, interval=pltInterval, repeat=False)
plt.show()

如果我的回答对你有帮助,请考虑接受它作为正确的回答。谢谢,但是我还有一些问题。这些绘图,我做的是实时传感器数据。如何为实时数据实现这一点。如果你愿意的话,我会把全部代码都发出去。再次感谢你的帮助。我实际上想做的是把所有这些实时图形放在一个带有开始和停止按钮的GUI中。因此,在将其放入GUI之前,尝试使用子包。我有单独绘图的精确代码。但是现在我需要GUI。最好将它们放在GUI中,还是将整个子地块放在GUI中。请帮帮我,我不确定你现在的状态。我的回答是,子地块的动画不起作用。我对GUI的了解还不够,无法为您提供建议。GUI没问题。指导我进行实时绘图的子绘图。你想看看代码吗?我当然看到了,但我无法想象它是如何工作的,尽管循环过程中有一个动画函数。我理解动画的基本思想是在图形初始化之后更新动画函数中的数据。
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

pltInterval = 50

t = np.arange(0.01, 5.0, 0.01)
ss1 = np.sin(2 * np.pi * t)
ss2 = np.sin(3 * np.pi * t)
ss3 = np.sin(4 * np.pi * t)
ss4 = np.sin(5 * np.pi * t)
yy = [ss1,ss2,ss3,ss4]
color = ['r', 'g', 'b', 'y'] 
title = ['Detector A', 'Detector B', 'Detector C', 'Detector D']

def example_plot(ax,i,y,k):
    ax.plot(t[:i], y[:i], color=color[k], linestyle='--')
    ax.set_xlabel('Time', fontsize=12)
    ax.set_ylabel('Detector Output', fontsize=12)
    ax.set_title(title[k], fontsize=14)
    
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)

def update(i):
    for k,(ax,y) in enumerate(zip(axs.flat,yy)):
        example_plot(ax,i,y,k)

anim = FuncAnimation(fig, update, frames=50, interval=pltInterval, repeat=False)
plt.show()