I'm plotting streamed data with tkinter but my app opens the current plot in an additional window:
My App looks something like this:
import tkinter as tk
from tkinter import ttk
from random import randint
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class App(tk.Tk):
def __init__(self):
super().__init__()
self.button_run = tk.Button(master=self, text='run', bg='grey', command=self.run)
self.button_run.pack()
self.fig = plt.Figure()
self.fig, self.axes_dict = plt.subplot_mosaic([['o', 'o'], ['_', '__']])
self.canvas = FigureCanvasTkAgg(figure=self.fig, master=self)
self.canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
self.canvas.draw_idle()
self.fig.canvas.flush_events()
def run(self):
S = Streamer(parent=self)
S.start()
And I stream data like this:
class Streamer:
def __init__(self, parent):
self.parent = parent
def start(self):
# plot random data
for x in range(6):
self.parent.axes_dict['o'].add_patch(Rectangle((x, randint(x, x*2)), height=0.4, width=0.4))
self.parent.axes_dict['o'].relim()
self.parent.axes_dict['o'].autoscale_view(True, True, True)
self.parent.fig.canvas.draw_idle()
self.parent.fig.canvas.flush_events()
plt.pause(0.4)
Starting the app:
if __name__ == '__main__':
A = App()
A.mainloop()
How can I close this matplotlib window and why does it appear?

plt.pause(0.4)from theStreamer class...And you are missing one=in starting the app it should be likeif __name__ == '__main__':