I was wondering how you can completely delete a plot from a tkinter window.
Assuming i would have a tkinter project like the following:
import tkinter as tk
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
data1 = {'Country': ['US','CA','GER','UK','FR'],
'GDP_Per_Capita': [45000,42000,52000,49000,47000]
}
df1 = DataFrame(data1,columns=['Country','GDP_Per_Capita'])
root= tk.Tk()
figure1 = plt.Figure(figsize=(6,5), dpi=100)
ax1 = figure1.add_subplot(111)
bar1 = FigureCanvasTkAgg(figure1, root)
bar1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
df1 = df1[['Country','GDP_Per_Capita']].groupby('Country').sum()
df1.plot(kind='bar', legend=True, ax=ax1)
ax1.set_title('Country Vs. GDP Per Capita')
def close_plot():
plt.close(figure1)
button_delete = Button(root, text='Delete', command = lambda: close_plot()).place(height=30, width = 100, rely=0.02, relx = 0.4)
root.mainloop()
I veen trying to use matplotlib.pyplot.close within a button but it doens't seem to work.
Anyone have a clue how to get rid of this plot.
Thank you very much!
Your figure is embedded in a tkinter widget. You need to keep a reference to that widget, and use its methods to add/remove it from the tkinter window:
Here we use pack_forget(): the widget is removed from the window, but still exists, and could be reused later.
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def remove_plot():
w.pack_forget() # here you remove the widget from the tk window
# w.destroy()
if __name__ == '__main__':
# data
x, y = [1, 2, 3, 4], [1, 4, 9, 16]
# matplotlib stuff
figure1 = plt.Figure(figsize=(6, 5), dpi=100)
ax1 = figure1.add_subplot(111)
ax1.plot(x, y)
ax1.set_title('Country Vs. GDP Per Capita')
# tkinter stuff
root = tk.Tk()
bar1 = FigureCanvasTkAgg(figure1, root)
w = bar1.get_tk_widget()
w.pack(side=tk.LEFT, fill=tk.BOTH) # here you insert the widget in the tk window
button_delete = tk.Button(root, text='Remove Plot', command=remove_plot)
button_delete.place(height=30, width=100, rely=0.02, relx=0.4) # place is an odd choice of geometry manager, you will have to adjust it every time the title changes
root.mainloop()
Alternatively, if you don't need that figure any longer, you could use w.destroy (commented line) to destroy the widget.
I would like to change the color of the toolbar when making a matplotlib figure in tkinter. I have managed to find and change the color of two parts. There is one remaining.
My code comes directly from https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_tk_sgskip.html?highlight=embedding%20tk with three additional lines to change colors.
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
color = "#d469a3"
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.config(background=color)
toolbar._message_label.config(background=color)
toolbar.update()
button = tkinter.Button(master=root, text="Quit", command=root.quit)
button.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
This gives me the window:
What is the small, grey rectangle I have pointed out? How do I change its color?
It is an empty label. You can get a reference to it via winfo_children:
print (toolbar.winfo_children()[-2])
# .!navigationtoolbar2tk.!label
And to change its color:
toolbar.winfo_children()[-2].config(background=color)
There are plenty of web examples (1,2,3,4) and threads (1,2,3) about imbedding a plot into a tkinter window, but very few that address plotting in a separate environment and importing the resulting graph to the tkinter window.
In a nutshell, I have a program that calculates many different values, and exports those values to a separate file that creates a large number of plots. My tkinter application accepts parameters in Entry boxes, before applying them to the main file that does all the calculations. Typically, I would just follow the examples I linked, but with such a large number of plots being generated and the need to be able to select any particular graph I need at a given time, this would be inefficient and time consuming to brute-force. There must be a better way!
Below is a simplified example of how I am trying to accomplish this task:
import tkinter as tk
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
import numpy as np
def example_plot(A):
# Plot generated outside of tkinter environment, but controlled by
# variable within tkinter window.
x = np.linspace(0, 10, 50)
y = A*x**2
fig, ax = plt.subplots()
ax.plot(x,y)
ax.set_xlabel('x')
ax.set_ylabel('y')
return fig
window = tk.Tk()
window.geometry('256x256')
variableEntry = tk.Entry(width = 10)
variableLabel = tk.Label(window, text = "A")
variableEntry.grid(row = 0, column = 0)
variableLabel.grid(row = 0, column = 1)
def plotButton():
A = variableEntry.get()
A = int(A)
figure = Figure(figsize = (1,1), dpi = 128)
add = figure.add_subplot(1,1,1)
example = example_plot(A)
add.imshow(example)
canvas = FigureCanvasTkAgg(figure)
canvas.get_tk_widget().grid(row = 2, column = 0)
toolbar = NavigationToolbar2Tk(canvas)
toolbar.update()
canvas._tkcanvas.grid(row = 3 , column = 0)
canvas.show()
applyButton = tk.Button(master = window, text = "Apply", command = plotButton)
applyButton.grid(row = 1,column = 0)
window.mainloop()
When I run this, set A to some integer and press apply, I get an error
TypeError: Image data of dtype object cannot be converted to float
It seems that add.imshow() doesn't like that I fed it the figure. Is there some way to obtain the figure (ie: example = example_plot(A)) and store it to display later?
Try this:
import tkinter as tk
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \
NavigationToolbar2Tk
import numpy as np
def example_plot(A):
# Plot generated outside of tkinter environment, but controlled by
# variable within tkinter window.
x = np.linspace(0, 10, 50)
y = A*x*x # This should run slightly faster
fig, ax = plt.subplots()
ax.plot(x,y)
ax.set_xlabel("x")
ax.set_ylabel("y")
return fig
window = tk.Tk()
frame = tk.Frame(window)
frame.pack()
variable_entry = tk.Entry(frame, width=10)
variable_label = tk.Label(frame, text="A")
variable_entry.pack(side="left", fill="x")
variable_label.pack(side="left")
def plot():
A = int(variable_entry.get())
figure = Figure(figsize=(1, 1), dpi=128)
add = figure.add_subplot(1, 1, 1)
figure = example_plot(A)
canvas = FigureCanvasTkAgg(figure)
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
canvas.get_tk_widget().pack()
# canvas.show() # There is no need for this
apply_button = tk.Button(window, text="Apply", command=plot)
apply_button.pack(fill="x")
window.mainloop()
Your example_plot returns a Figure so you can use figure = example_plot(A) and then FigureCanvasTkAgg(figure). I also added a frame and tried to make everything look better.
I am trying to make an interactive plotting GUI using Tkinter and matplotlib (python 3.7 and matplotlib 3.0.0) I want the user to be able to resize the figure as it is displayed on the screen without resizing the window, and have achieved this by editing the dpi, width, and height properties of the figure. So far, this works, but if the figure is bigger than the display area, I want the user to be able to scroll to see the whole figure. And if the figure is smaller than the display area, I want the scrollbars to be disabled.
I have tried applying scrollbars directly to the FigureCanvasTkAgg object itself as well as embedding the FigureCanvasTkAgg canvas inside a second scrollable canvas, but it seems that the problem is that the drawable area of the FigureCanvasTkAgg widget doesn't change when the figure size changes. Minimal code reproducing the problem is below. Is there some property of the FigureCanvasTkAgg object that I'm missing that makes this work?
import tkinter as tk
from tkinter import ttk
from tkinter.simpledialog import askfloat
from matplotlib.figure import Figure
from matplotlib.axes import Axes
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class InteractivePlot(tk.Frame):
def __init__(self,master,**kwargs):
super().__init__(master,**kwargs)
self._figure = Figure(dpi=150)
self._canvas = FigureCanvasTkAgg(self._figure, master=self)
self._sizebutton = tk.Button(self,text="Size (in.)", command=self._change_size)
self._axis = self._figure.add_subplot(111)
# Plot some data just to have something to look at.
self._axis.plot([0,1,2,3,4,5],[1,1,3,3,5,5],label='Dummy Data')
self._cwidg = self._canvas.get_tk_widget()
self._scrx = ttk.Scrollbar(self,orient="horizontal", command=self._cwidg.xview)
self._scry = ttk.Scrollbar(self,orient="vertical", command=self._cwidg.yview)
self._cwidg.configure(yscrollcommand=self._scry.set, xscrollcommand=self._scrx.set)
self._cwidg.bind(
"<Configure>",
lambda e: self._cwidg.configure(
scrollregion=self._cwidg.bbox("all")
)
)
self._sizebutton.grid(row=0,column=0,sticky='w')
self._cwidg. grid(row=1,column=0,sticky='news')
self._scrx. grid(row=2,column=0,sticky='ew')
self._scry. grid(row=1,column=1,sticky='ns')
self.rowconfigure(1,weight=1)
self.columnconfigure(0,weight=1)
self._canvas.draw()
def _change_size(self):
newsize = askfloat('Size','Input new size in inches')
if newsize is None:
return
w = newsize
h = newsize/1.8
self._figure.set_figwidth(w)
self._figure.set_figheight(h)
self._canvas.draw()
root = tk.Tk()
plt = InteractivePlot(root,width=400,height=400)
plt.pack(fill=tk.BOTH,expand=True)
root.mainloop()
The main issue here is that the matplotlib figure is meant to resize with self._cwidg. Because the figure is assumed to always be of the same size as self._cwidg, matplotlib only redraws the portion of the figure which is visible in self._cwidg and the latter is not resized when the figure size changes.
A work around is to use an extra canvas self._scroll_canvas and embed self._cwidg as a window inside it. Then I modified _change_size() in the following way:
def _change_size(self):
newsize = askfloat('Size', 'Input new size in inches')
if newsize is None:
return
w = newsize
h = newsize/1.8
self._cwidg.configure(width=int(w*self._conv_ratio), height=int(h*self._conv_ratio))
self._scroll_canvas.configure(scrollregion=self._scroll_canvas.bbox("all"))
I directly resize self._cwidg which in turns resizes the figure, ensuring that every part of it is redrawn. Then I update the scrollregion. Here is the full code:
import tkinter as tk
from tkinter import ttk
from tkinter.simpledialog import askfloat
from matplotlib.figure import Figure
from matplotlib.axes import Axes
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class InteractivePlot(tk.Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self._scroll_canvas = tk.Canvas(self)
self._figure = Figure(dpi=150)
self._canvas = FigureCanvasTkAgg(self._figure, master=self._scroll_canvas)
self._sizebutton = tk.Button(self, text="Size (in.)", command=self._change_size)
self._axis = self._figure.add_subplot(111)
# Plot some data just to have something to look at.
self._axis.plot([0, 1, 2, 3, 4, 5], [1, 1, 3, 3, 5, 5], label='Dummy Data')
self._cwidg = self._canvas.get_tk_widget()
self._scroll_canvas.create_window(0, 0, anchor='nw', window=self._cwidg)
self._scrx = ttk.Scrollbar(self, orient="horizontal", command=self._scroll_canvas.xview)
self._scry = ttk.Scrollbar(self, orient="vertical", command=self._scroll_canvas.yview)
self._scroll_canvas.configure(yscrollcommand=self._scry.set, xscrollcommand=self._scrx.set)
self._sizebutton.grid(row=0, column=0, sticky='w')
self._scroll_canvas.grid(row=1, column=0, sticky='news')
self._scrx.grid(row=2, column=0, sticky='ew')
self._scry.grid(row=1, column=1, sticky='ns')
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self._canvas.draw()
wi = self._figure.get_figwidth()
wp = self._cwidg.winfo_reqwidth(),
self._conv_ratio = wp / wi # get inch to pixel conversion factor
self._scroll_canvas.configure(width=wp, height=self._cwidg.winfo_reqheight())
self._scroll_canvas.configure(scrollregion=self._scroll_canvas.bbox("all"))
def _change_size(self):
newsize = askfloat('Size', 'Input new size in inches')
if newsize is None:
return
w = newsize
h = newsize/1.8
self._cwidg.configure(width=int(w*self._conv_ratio), height=int(h*self._conv_ratio))
self._scroll_canvas.configure(scrollregion=self._scroll_canvas.bbox("all"))
root = tk.Tk()
plt = InteractivePlot(root, width=400, height=400)
plt.pack(fill=tk.BOTH, expand=True)
root.mainloop()
Thanks to j_4321's answer, and after digging around in the source code for the FigureCanvasTk object, I came up with the following solution that does everything I need it to.
It looks like what the canvas object does is use tkinter.Canvas.create_image to generate a new image of the figure every time the canvas widget is resized, keeping the DPI of the figure constant and setting the width and height of the figure to keep it the same size as the canvas widget. Since I want the canvas widget to resize to the figure, I calculate the width and height from the figure properties and then pass a custom Event object to FigureCanvasTk.resize, the callback function for the canvas widget's <Configure> event.
The last trick is to set the scrollregion of the canvas to be only the size of the last canvas item created. It looks like previous iterations of the figure are not deleted from the canvas (seems like a memory leak?), so if you try to set the scrollregion to Canvas.bbox('all'), it will set the scrollregion to the size of the largest version of the figure, not the current version of the figure.
Here is the full example code:
import tkinter as tk
from tkinter import ttk
from tkinter.simpledialog import askfloat
from matplotlib.figure import Figure
from matplotlib.axes import Axes
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class InteractivePlot(tk.Frame):
def __init__(self,master,**kwargs):
super().__init__(master,**kwargs)
self._figure = Figure(dpi=150)
self._canvas = FigureCanvasTkAgg(self._figure, master=self)
buttonframe = tk.Frame(self)
self._sizebutton = tk.Button(buttonframe,text="Size (in.)", command=self._change_size)
self._dpibutton = tk.Button(buttonframe,text="DPI", command=self._change_dpi)
self._axis = self._figure.add_subplot(111)
# Plot some data just to have something to look at.
self._axis.plot([0,1,2,3,4,5],[1,1,3,3,5,5],label='Dummy Data')
self._cwidg = self._canvas.get_tk_widget()
self._scrx = ttk.Scrollbar(self,orient="horizontal", command=self._cwidg.xview)
self._scry = ttk.Scrollbar(self,orient="vertical", command=self._cwidg.yview)
self._cwidg.configure(yscrollcommand=self._scry.set, xscrollcommand=self._scrx.set)
self._cwidg.bind("<Configure>",self._refresh)
self._sizebutton.grid(row=0,column=0,sticky='w')
self._dpibutton.grid(row=0,column=1,sticky='w')
buttonframe.grid(row=0,column=0,columnspan=2,sticky='W')
self._cwidg. grid(row=1,column=0,sticky='news')
self._scrx. grid(row=2,column=0,sticky='ew')
self._scry. grid(row=1,column=1,sticky='ns')
self.rowconfigure(1,weight=1)
self.columnconfigure(0,weight=1)
# Refresh the canvas to show the new plot
self._canvas.draw()
# Figure size change button callback
def _change_size(self):
newsize = askfloat('Size','Input new size in inches')
if newsize is None:
return
w = newsize
h = newsize/1.8
self._figure.set_figwidth(w)
self._figure.set_figheight(h)
self._refresh()
# Figure DPI change button callback
def _change_dpi(self):
newdpi = askfloat('DPI', 'Input a new DPI for the figure')
if newdpi is None:
return
self._figure.set_dpi(newdpi)
self._refresh()
# Refresh function to make the figure canvas widget display the entire figure
def _refresh(self,event=None):
# Get the width and height of the *figure* in pixels
w = self._figure.get_figwidth()*self._figure.get_dpi()
h = self._figure.get_figheight()*self._figure.get_dpi()
# Generate a blank tkinter Event object
evnt = tk.Event()
# Set the "width" and "height" values of the event
evnt.width = w
evnt.height = h
# Set the width and height of the canvas widget
self._cwidg.configure(width=w,height=h)
self._cwidg.update_idletasks()
# Pass the generated event object to the FigureCanvasTk.resize() function
self._canvas.resize(evnt)
# Set the scroll region to *only* the area of the last canvas item created.
# Otherwise, the scrollregion will always be the size of the largest iteration
# of the figure.
self._cwidg.configure(scrollregion=self._cwidg.bbox(self._cwidg.find_all()[-1]))
root = tk.Tk()
plt = InteractivePlot(root,width=400,height=400)
plt.pack(fill=tk.BOTH,expand=True)
root.mainloop()
I'm making a Tkinter GUI and I'm trying to get visuals for it. I want to plot some seaborn graphs on a scrolling canvas so that the graphs can be sized properly. I make a figure and an axes list using matplotlib.pyplot.subplots and then put it onto a FigureCanvasTkAgg which I attach two scrollbars to. I can scroll all over the canvas but the figure remains in the a small part of the canvas.
How can I get the figure to take up the entire area of the canvas and then scroll to the different parts?
Basically I want the output achieved here: Scrollbar on Matplotlib showing page
but instead of PyQt using Tkinter.
import sys
import tkinter as tk
from tkinter import ttk
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
matplotlib.use("TkAgg")
from matplotlib import style
style.use("ggplot")
plt.rcParams.update({'font.size': 12})
root = tk.Tk()
# frame = ttk.Frame(root)
# frame.pack(expand = True)
combMapFig, combMapAxes = plt.subplots(nrows = 5, ncols = 10, figsize = (100,100), dpi = 100)
# axes_list = axes.flatten()
# axes_list = axes_list.tolist()
combMapAxes = combMapAxes.tolist()
# plt.gca().set_position([0,0,1,1])
mapCanvas = FigureCanvasTkAgg(combMapFig, root)
mapCanvas.draw()
mapXScroll = tk.Scrollbar(root, orient = 'horizontal')
mapXScroll.config(command = mapCanvas.get_tk_widget().xview)
mapXScroll.pack(side = tk.BOTTOM, fill = tk.X, expand = tk.FALSE)
mapYScroll = tk.Scrollbar(root)
mapYScroll.config(command = mapCanvas.get_tk_widget().yview)
mapYScroll.pack(side = tk.RIGHT, fill = tk.Y, expand = tk.FALSE)
mapCanvas.get_tk_widget().config(yscrollcommand = mapYScroll.set)
mapCanvas.get_tk_widget().config(xscrollcommand = mapXScroll.set)
# mapCanvas.get_tk_widget().config(scrollregion = mapCanvas.get_tk_widget().bbox("all"))
mapCanvas.get_tk_widget().config(width = 500, height = 500)
mapCanvas.get_tk_widget().config(scrollregion = (0,0, 5000,5000))
mapCanvas.get_tk_widget().pack(side = tk.LEFT, expand = True, fill = tk.BOTH)
mapCanvas._tkcanvas.pack(side = tk.LEFT, expand = True, fill = tk.BOTH)
root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
try:
root.mainloop()
except KeyboardInterrupt as e:
sys.exit("Keyboard Interrupt. Program Closed")
Current Output
Blank Canvas where the figure should expand to
Thanks