OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Tkinter: resizing figure, old one visible below

  • Thread starter Thread starter AlMa
  • Start date Start date
A

AlMa

Guest
I have a tkinter application with a canvas containing a figure. I am trying to change the height of the figure.

I created an entry field ("figure_height_entry") to get the figure height from the user. Upon pressing "Enter", the figure is resized using the function update_figure_size().

The resizing works but if making the figure smaller (let's say by entering "500"), the old figure, larger, is still visible below.

I tried to add fig.clear() in the update_figure_size() function, it gives a blank square with the old figure still visible below.

How can I remove/hide the old figure when resizing it?

MWE:

Code:
from tkinter import Label, Tk, IntVar, Entry
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def plot_data():
    ax.clear()
    ax.plot([20,22,24,26,28],[5,10,8,3,6])
    canvas.draw_idle()
    
def update_figure_size():
    fig.set_size_inches(700*px, figure_height.get()*px)
    plot_data()
    
# MAIN CODE

root = Tk()

px = 1/plt.rcParams['figure.dpi']# with default figure.dpi = 100
figure_height = IntVar(value=600)
figure_height_entry = Entry(master=root,textvariable=figure_height)
        
fig = plt.Figure(figsize=(700*px, figure_height.get()*px))
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.get_tk_widget().grid(column=0, row=0)

label = Label(root, text="Figure height:")
label.grid(column=1,row=0)

figure_height_entry.bind('<Return>', lambda e: update_figure_size())
figure_height_entry.grid(column=2,row=0)  

plot_data()

root.mainloop()
<p>I have a <code>tkinter</code> application with a <code>canvas</code> containing a figure. I am trying to change the height of the figure.</p>
<p>I created an <code>entry</code> field ("figure_height_entry") to get the figure height from the user. Upon pressing "Enter", the figure is resized using the function <code>update_figure_size()</code>.</p>
<p>The resizing works but if making the figure smaller (let's say by entering "500"), the old figure, larger, is still visible below.</p>
<p>I tried to add <code>fig.clear()</code> in the <code>update_figure_size()</code> function, it gives a blank square with the old figure still visible below.</p>
<p>How can I remove/hide the old figure when resizing it?</p>
<p><strong>MWE:</strong></p>
<pre><code>from tkinter import Label, Tk, IntVar, Entry
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def plot_data():
ax.clear()
ax.plot([20,22,24,26,28],[5,10,8,3,6])
canvas.draw_idle()

def update_figure_size():
fig.set_size_inches(700*px, figure_height.get()*px)
plot_data()

# MAIN CODE

root = Tk()

px = 1/plt.rcParams['figure.dpi']# with default figure.dpi = 100
figure_height = IntVar(value=600)
figure_height_entry = Entry(master=root,textvariable=figure_height)

fig = plt.Figure(figsize=(700*px, figure_height.get()*px))
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.get_tk_widget().grid(column=0, row=0)

label = Label(root, text="Figure height:")
label.grid(column=1,row=0)

figure_height_entry.bind('<Return>', lambda e: update_figure_size())
figure_height_entry.grid(column=2,row=0)

plot_data()

root.mainloop()
</code></pre>
 
Top