I try to implement matplotlib chart into tkinter app and it works but after closing main window (m) the process is still going and cant stop it without closing the console. Why?
import matplotlib.backends.backend_tkagg as tkagg
import matplotlib.pyplot as plt
import matplotlib
import tkinter as tk
matplotlib.use('TkAgg')
def plot_canvas(master):
points = [(0, 0), (10, 0), (10, 25), (20, 25), (25, 0)]
fig = plt.figure(figsize=(5, 4))
fig.add_subplot(111).plot([x[0] for x in points],
[x[1] for x in points],
marker='.',
linestyle='-')
# ax.
# ax.plot([10, 20], [25, 25], marker='.', linestyle=':', color='green')
return tkagg.FigureCanvasTkAgg(fig, master=master).get_tk_widget()
m = tk.Tk()
wgt = plot_canvas(m)
wgt.grid()
m.mainloop()
There are two solutions:
change
matplotlib.use('TkAgg')tomatplotlib.use('Agg')use
matplotlib.figure.Figure(...)instead ofplt.figure(...)