0

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

1 Answers1

2

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

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • If my answer helped you, please consider accepting it as the correct answer – r-beginners May 17 '21 at 13:34
  • Thanks man but I have some further questions. These plotings, am doing it for real time sensor data. How do I implement this for real time data. if you want, I'll post the whole code. And once again, thanks for your help. What am actually trying to do is put all these live graphs in a GUI with a start and a stop button. So, trying for subplot before putting it in the GUI. I have the precise code for individual plotting. But now I need the GUI. Is it better to place these in a GUI or placing the entire subplot in the GUI. please help me out. – Abhinav kumar May 17 '21 at 13:50
  • I'm not sure what your current state is. I am answering with the understanding that the animation of the subplot is not working. I don't know enough about GUIs to be able to advise you. – r-beginners May 17 '21 at 14:03
  • It's fine with the GUI. Guide me for this subplot of real time plotting. Do you want to look at the code? – Abhinav kumar May 17 '21 at 14:10
  • Of course I see it, but I can't imagine how it works, although there is an animation function in the loop process. I understand that the basic idea of animation is to update the data in the animation function after the initialization of the graph. – r-beginners May 18 '21 at 02:08