【问题标题】:Function for subplots for real time plotting用于实时绘图的子图功能
【发布时间】:2021-05-17 10:50:06
【问题描述】:

我有一个代码,它为我提供了 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
    pltInterval = 50  # Period at which the plot animation updates [ms]
    lineLabelText = ['Detector A', 'Detector B', 'Detector C', 'Detector D']
    title = ['Detector A', 'Detector B', 'Detector C', 'Detector D']
    xLimit = [(0, maxPlotLength), (0, maxPlotLength), (0, maxPlotLength), (0, maxPlotLength)]
    yLimit = [(-1, 1), (-1, 1), (-1, 1), (-1, 1)]
    style = ['r-', 'g-', 'b-', 'y-']  # linestyles for the different plots
    anim = []
    for i in range(numPlots):
        fig, ax = makeFigure(xLimit[i], yLimit[i], title[i])
        lines = ax.plot([], [], style[i], label=lineLabelText[i])[0]
        timeText = ax.text(0.50, 0.95, '', transform=ax.transAxes)
        lineValueText = ax.text(0.50, 0.90, '', transform=ax.transAxes)

        anim.append(
            animation.FuncAnimation(fig, s.getSerialData, fargs=(lines, lineValueText, lineLabelText[i], timeText, i),
                                    interval=pltInterval))  # fargs has to be a tuple

        plt.legend(loc="upper left")
    plt.show()

    s.close()

【问题讨论】:

    标签: matplotlib subplot matplotlib-animation


    【解决方案1】:

    我已经创建了四个子图的动画,尽管它们不满足您正在寻找的所有图形要求。动画函数 (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 了解不多,无法为您提供建议。
    • 图形用户界面没问题。指导我了解这个实时绘图的子图。要不要看代码?
    • 我当然看到了,但是我无法想象它是如何工作的,虽然在循环过程中有一个动画功能。我理解动画的基本思想是在图初始化后更新动画函数中的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多