The python code below was provided by NMech here.
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import numpy as np
class Application(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
self.createWidgets()
def createWidgets(self):
fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0.1,0.1,0.8,0.8], polar=True)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0,column=1)
self.plotbutton = tk.Button(master=root, text="plot", command=lambda: self.plot(canvas, ax))
self.plotbutton.grid(row=0, column=0)
def plot(self, canvas, ax):
c= ['r','b','g']
ax.clear()
for i in range(3):
theta = np.random.uniform(0,360,10)
r = np.random.uniform(0,1,10)
ax.plot(theta,r,linestyle="None",marker='o',color=c[i])
canvas.draw()
root=tk.Tk()
app=Application(master=root)
app.mainloop()
The code displays two similar windows shown below. One window is the app labeled 'tk', and the second window is labeled 'Figure 1'. How do I modify the code so it only displays the app 'tk'?
Bryan Oakley
388k53 gold badges580 silver badges738 bronze badges
asked Jan 8, 2024 at 15:47
default
fig = plt.figure
withfig = matplotlib.figure.Figure
, then it worked as expected. As I understand it,pyplot
always does window management for you but in this case you are organising that for yourself so you don't want to usepyplot
at all.