'Function for subplots for real time plotting

I have a code which gives me 4 individual live plotting graphs for 4 different sensors. But, I want them in a single frame using subplots. Pasting 2 pieces of code which are plotting my graphs. How should I modify them for my output to be a subplots of 4 live graphs.

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()


Solution 1:[1]

I have created animations of the four subplots, although they do not meet all the graphical requirements you are looking for. The animation function (i) calls the plotting function (k). I don't have much experience with this kind of animation, so I hope it will help you in your development.

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()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r-beginners