Невозможно встроить несколько анимированных графиков matplotlib в окно tkinter.Python

Программы на Python
Гость
Невозможно встроить несколько анимированных графиков matplotlib в окно tkinter.

Сообщение Гость »


При первом нажатии кнопки «Живые вторичные графики миссий» график данных вторичного графика внедряется в графический интерфейс пользователя и анимируется, однако создаются два пустых основных рисунка, один из которых представляет собой график, созданный 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. , 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 ctk
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from data_processing.primary_graph_data import PrimaryGraphData
from data_processing.secondary_graph_data import SecondaryGraphData

class 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. если

Код: Выделить всё

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


Источник: https://stackoverflow.com/questions/781 ... ter-window

Вернуться в «Python»