Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

Cannot embed multiple animated matplotlib graphs in tkinter window

$
0
0

When first clicking the "Live Secondary Mission Graphs" button, the secondary graph data plot is embedded in the GUI and is animated, however two blank primary figures are created, one is the graph created by the primary object and the other is the graph created by the secondary object. When clicking the "Live Primary Mission Graphs" button afterwards, it reloads the graphs but no new graph is plotted in the GUI, and i get an error message in the terminal saying:

UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. anim, that exists until you output the Animation using plt.show() or anim.save().

here is the module used to create the GUI

import customtkinter as ctkfrom matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)from data_processing.primary_graph_data import PrimaryGraphDatafrom data_processing.secondary_graph_data import SecondaryGraphDataclass App(ctk.CTk):    def __init__(self) -> None:        super().__init__()        self.geometry(f"{1100}x{580}")        self.tabview = ctk.CTkTabview(self, width=1080, height=570)        self.tabview.pack()        self.tabview.add("Live Primary Mission Graphs")        self.tabview.add("Live Secondary Mission Graphs")        #LIVE GRAPHS SETUP        primary = PrimaryGraphData()        canvas_primary = FigureCanvasTkAgg(primary.primaryfig, self.tabview.tab("Live Primary Mission Graphs"))        canvas_primary.draw()        canvas_primary.get_tk_widget().grid(row=0, column=0)        primary_graphs_button = ctk.CTkButton(self.tabview.tab("Live Primary Mission Graphs"), text="Start Primary Mission Graphs", command=lambda:primary.plot_primary_graphs())        primary_graphs_button.place(x=850, y=190)        secondary = SecondaryGraphData()        canvas_secondary = FigureCanvasTkAgg(secondary.secondaryfig, self.tabview.tab("Live Secondary Mission Graphs"))        canvas_secondary.draw()        canvas_secondary.get_tk_widget().grid(row=0, column=0)        secondary_graphs_button = ctk.CTkButton(self.tabview.tab("Live Secondary Mission Graphs"), text="Start Secondary Mission Graphs", command=lambda: secondary.plot_secondary_graphs())        secondary_graphs_button.place(x=850, y=190)

I have tried switching around the order in which the code to display each of the graphs displays, and it only allows me to display the last bit of run code e.g. if the primary_graphs_button line is executed after the secondary_graphs_botton line, that button will work in displaying the graphs.

the classes I used to crate the matplotlib graphs are below:

class PrimaryGraphData():    def __init__(self) -> None:        self.PRIMARYDATASOURCE = "data.csv"        self.primaryfig, self.primaryaxs = plt.subplots(2, 2)    def animate(self, i):        data = pd.read_csv(self.PRIMARYDATASOURCE)        time = data["Time"]        temp = data["Temperature"]        press = data["Pressure"]        alt = data["Altitude"]        plt.cla()        self.primaryaxs[0, 0].plot(time, temp, "tab:orange")        self.primaryaxs[0, 1].plot(time, press, "tab:green")        self.primaryaxs[1, 0].plot(alt, temp)        self.primaryaxs[1, 1].plot(time, alt, "tab:blue")    def plot_primary_graphs(self):        ani = FuncAnimation(plt.gcf(), func=self.animate, interval=500, frames=50)        plt.tight_layout()        plt.show()
class SecondaryGraphData():    def __init__(self) -> None:        self.SECONDARYDATASOURCE = "data2.csv"        self.secondaryfig, self.secondaryaxs = plt.subplots(2, 2)    def secondaryanimate(self, i):        data = pd.read_csv(self.SECONDARYDATASOURCE)        time = data["Time"]        alt = data["Altitude"]        TVOC = data["TVOC"]        eCO2 = data["eCO2"]        H2 = data["H2"]        Ethanol = data["Ethanol"]        plt.cla()        # Plot subplots        self.secondaryaxs[0, 0].plot(time, TVOC, "tab:green")        self.secondaryaxs[0, 1].plot(alt, eCO2, "tab:green")        self.secondaryaxs[1, 0].plot(alt, H2)        self.secondaryaxs[1, 1].plot(time, Ethanol, "tab:blue")    def plot_secondary_graphs(self):        anisecondary = FuncAnimation(plt.gcf(), func=self.secondaryanimate, interval=500,frames=50)        plt.tight_layout()        plt.show()

Viewing all articles
Browse latest Browse all 14040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>