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.
Related
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'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
I am trying to design a python gui to be able to assess impacts of motion by plotting brain slices, the framewise displacement timeseries, and different outputs of motion detection algorithms. I want to be able to slide through each of the brain volumes individually (180 volumes per scan) so that I can compare the FD timecourse to what the actual brain data looks like.
I've been using tkinter and I can plot several slices of one brain volume, but I'm having updating volume that is selected. I've tried creating buttons to advance and go back, and also using a tkinter Scale.
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
import os
import nibabel
from nilearn import plotting
from nilearn import image
from matplotlib.widgets import Slider, Button, RadioButtons
data_path = os.getcwd()
file='sub-HV01baseline_task-EA1_bold.nii'
file_path = os.path.join(data_path,file)
EA1=nibabel.load(file_path)
struct_arr2 = EA1.get_data()
vol=1
import tkinter as tk
from tkinter import ttk
fig = plt.Figure(figsize=(10,5), dpi=100)
class App:
def __init__(self, master):
self.event_num = 1
frame = tk.Frame(master)
frame.pack()
self.txt = tk.Entry(frame,width=10)
self.txt.pack(side="bottom")
self.lbl = tk.Label(frame, text="FD Value")
self.lbl.pack(side="bottom")
self.btn = tk.Button(frame, text = "Update",command=self.clicked)
self.btn.pack(side="bottom")
self.txt.focus()
self.var =tk.IntVar(frame)
self.var.set(0)
self.vol_scale=tk.Scale(frame,from_=0, to=180,orient="horizontal",sliderlength=20,command=self.show_slices(fig))
self.increase_btn = tk.Button(frame, text = "Increase",command=self.show_slices(fig))
self.increase_btn.pack(side="bottom")
self.vol_scale.pack(side="bottom")
#self.spin = tk.Spinbox(frame, from_=0, to=180, width=5, textvariable=self.var)
#self.spin.pack(side="bottom")
self.canvas = FigureCanvasTkAgg(fig,master=master)
self.canvas.get_tk_widget().pack(side=tk.TOP)
def clicked(self):
res = "FD = " + self.txt.get()
self.lbl.configure(text = res)
def show_slices(self,fig):
vol = self.vol_scale.get()
slice_0 = struct_arr2[:, :, 10,vol]
slice_1 = struct_arr2[:, : , 15,vol]
slice_2 = struct_arr2[:, :, 20,vol]
slice_3 = struct_arr2[:, :, 25,vol]
slice_4 = struct_arr2[:, : , 30,vol]
slices=[slice_0, slice_1, slice_2, slice_3, slice_4]
axes = fig.subplots(1, len(slices))
#svol = Slider(axes, 'Vol', 0, 180, valinit=0, valstep=1)
fig.subplots_adjust(hspace=0, wspace=0)
for i, slice in enumerate(slices):
axes[i].xaxis.set_major_locator(plt.NullLocator())
axes[i].yaxis.set_major_locator(plt.NullLocator())
axes[i].imshow(slice.T, origin="lower")
root=tk.Tk()
app = App(root)
root.mainloop()
Currently I'm getting an error that "App has no attribute 'vol_scale'" even though I've defined it above.
I am trying to plot a graph using Matplotlib in tkinter. Here the graph should plot all the values of 'a' in the range 0-24. My code is as follows
import math
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
def att_func(d=0, n=0, z=0):
# Getting User inputs from the UI
d = d_user.get()
n = n_user.get()
z = z_user.get()
a = (-9.87 * math.sin(2 * ((2 * math.pi * (d - 81)) / 365)) + n * z)
a_label.configure(text=a)
return (a)
#Plotting the graph
class App:
def __init__(self, master):
frame = tkinter.Frame(master)
self.nbutton_graph = tkinter.Button(frame, text="Show Graph", command=self.graph)
self.nbutton_graph.pack()
f = Figure(figsize=(5, 5), dpi=100)
ab = f.add_subplot(111)
self.line, = ab.plot(range(24))
self.canvas = FigureCanvasTkAgg(f, self)
self.canvas.show()
self.canvas.get_tk_widget().pack()
def graph(self):
day_elevation_hrs = []
for i in range(24):
day_elevation_hrs.append(att_func(i, 0, 0)[0])
self.canvas.draw()
return
root = tkinter.Tk()
app = App(root)
# User Inputs
d_user = IntVar()
n_user = DoubleVar()
z_user = DoubleVar()
nlabel_d = Label(text="Enter d").pack()
nEntry_d = Entry(root, textvariable=d_user).pack()
nlabel_n = Label(text="Enter n").pack()
nEntry_n = Entry(root, textvariable=n_user).pack()
nlabel_z = Label(text="Enter z").pack()
nEntry_z = Entry(root, textvariable=z_user).pack()
# Displaying results
nlabel_a = Label(text="a is").pack()
a_label = Label(root, text="")
a_label.pack()
root.mainloop()
Here I am able to calculate what i need. But when I am trying to plot the same, I am unable to. I tried as many modification I could. But seems to be in a stale mate. I am sure that I am going wrong somewhere. but can't figure out where.
when i try to plot the same graph with matplotlib, with out tkinter, it works. but when I try to do it in a UI with tkinter i am unable to.. Here is the code for plotting graph in matplotlib without tkinter.
import matplotlib.pylab as pl
day_elevation_hrs=[]
for i in range(24):
day_elevation_hrs.append(att_func(i, 0, 0)[0])
pl.title("Elevation of a in range i")
pl.plot(day_elevation_hrs)
Your code won't run as posted, but I can see two definite problems.
First, your App is a frame that contains a canvas, but you never add the frame to the root window. Because of that, your canvas will be invisible.
Add the following code after you create an instance of App:
app.pack(side="top", fill="both", expand=True)
Second, you are making a common mistake when defining the button to display the graph. The command attribute takes a reference to a function. However, you are calling the graph() function, and using the result as the value of the command attribute.
In other words, change this:
self.nbutton_graph = Tk.Button(self, text="Show Graph", command=self.graph())
to this:
self.nbutton_graph = Tk.Button(self, text="Show Graph", command=self.graph)
Notice the lack of () after self.graph. This could be the reason why you are seeing errors like 'App' object has no attribute 'line', since you are calling the graph function before you fully initialize all your variables.
This documentation shows that the second explicit parameter for FigureCanvasTkAgg.__init__ should be the master. (It's really a keyword parameter.)
So, have you tried changing that line to ...
self.canvas = FigureCanvasTkAgg(f, master=master)
I'm working on curve fitting using the Radial Basis Neural Network. I have succeeded in embedding my plot to the tkinter canvas after following this guide.
My problem however is that when I run my code, the plot does not display in the canvas unless I either expand or contract the GUI window. I have searched for related problems here but I couldn't find one addressing specifically this issue. I am using python 3.4.2.
from tkinter import *
import math
import numpy as np
from numpy import *
import scipy
import scipy.linalg
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
class RadialBasisNetwork:
def __init__(self):
window = Tk()
window.title("RBF Curve Fitting")
self.f = Figure(figsize=(4,3), dpi=100)
self.canvas = FigureCanvasTkAgg(self.f, master=window)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=LEFT, fill=BOTH, expand=1)
self.canvas._tkcanvas.pack(side=LEFT, fill=BOTH, expand=1)
frame0 = Frame(window)
frame0.pack(fill=BOTH, expand=1)
title = Label(frame0, text = "RBF Network Controller", font = "Times 12 bold")
title.grid(row = 1, column = 1)
def Plot(self):
x = np.linspace(x_0, x_1, num = steps) # generate domain of x
plot_title = "model of " + model
a = self.f.add_subplot(111)
a.plot(x,ypred, 'r--') # plot x against predicted y
a.plot(x,y) # plot x against actual y (target)
a.legend(["Fit", "Target"], loc = "best", frameon = False, labelspacing = 0)
a.set_title(plot_title)
a.set_ylabel('Y axis')
a.set_xlabel('X axis')
RadialBasisNetwork()