'How to run two separate simultaneous Matplotlib.Funcanimation on Tkinter?

I am building a GUI with Tkinter and need to display two distinct live graphs, each individually built with Matplotlib.Funcanimation. As the two graphs are representing two very different sections of the application, each graph is built in a different class object. I am looking for a workaround to have both of them running together without having to put them into the same class and the same Funcanimation (or part of the same Figure Subplot). Would multithreading be a viable option for example?

Here is a simplified code snippet outlying what I am trying to achieve:

class first_app(tk.Frame):
    def __init__(self, parent, controller):
        self.x = []
        self.y = []
        self.fig, self.ax = plt.subplots(1,1)
        self.line, = self.ax.plot([], [], '.k')
        self.ax.set_xticks([0,500,1000,1500])
        self.ax.set_yticks([0,500,1000,1500])
        label = Tk.Label(parent ,text="Simulation 1").grid(column = 0, row = 0)
        canvas = FigureCanvasTkAgg(self.fig, master=parent)
        canvas.get_tk_widget().grid(column = 0, row = 1)
        ani = animation.FuncAnimation(self.fig, self.animate, interval=25, blit=False)
        
    def animate(self, i):
        self.x.append(i)
        self.y.append(i*2)
        self.line.set_data(self.x, self.y)

class second_app(tk.Frame):
    def __init__(self, parent, controller):
        self.x = []
        self.y = []
        self.fig, self.ax = plt.subplots(1,1)
        self.line, = self.ax.plot([], [], '.r')
        self.ax.set_xticks([0,500,1000,1500])
        self.ax.set_yticks([0,500,1000,1500])
        label = Tk.Label(parent ,text="Simulation 2").grid(column = 0, row = 0)
        canvas = FigureCanvasTkAgg(self.fig, master=parent)
        canvas.get_tk_widget().grid(column = 0, row = 1)
        ani = animation.FuncAnimation(self.fig, self.animate, interval=25, blit=False)
        
    def animate(self, i):
        self.x.append(i)
        self.y.append(i*2)
        self.line.set_data(self.x, self.y)

class main_window(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.wm_title("Test Application")
        frame1 = tk.Frame(self)
        frame1.grid(column = 0, row = 0)
        frame2 = tk.Frame(self)
        frame2.grid(column = 1, row = 0)
        
        first_app(frame1, self)
        second_app(frame2, self)
        
if __name__ == "__main__":
    testObj = main_window()
    testObj.mainloop() 


Sources

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

Source: Stack Overflow

Solution Source