Matplotlib Event Handlers not being Called when Embedded in Tkinter - python

Overview
I am in the process of embedding a Matplotlib plot in a Tkinter window. I need to use the Matplotlib event handler functions (described here). When run as a standalone Matplotlib figure, I get the correct behavior: the event handlers perform their correct function on the correct user action. But when embedding in a Tkinter window, the Matplotlib event handlers are no longer being called.
Expected Behavior
The Matplotlib event handler should be called when the figure is embedded in a Tkinter window.
Current Behavior
The Matplotlib event handler is not being called.
Minimal Code Snippet
Without Tkinter
import matplotlib.pyplot as plt
def onpick(event):
print("You selected the line!")
if __name__=='__main__':
### MATPLOTLIB SETUP ###
xs = [0,1,2] #x-values of the graph
ys = [4,3,2] #y-values of the graph
fig, ax = plt.subplots(1)
ax.plot(xs, ys, picker=True)
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
When this code is run, you can select the line on the plot and the onpick method is called, printing the text to stdout. This is the desired output.
Embedded in Tkinter
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
def onpick(event):
print("You selected the line!")
if __name__=='__main__':
### MATPLOTLIB SETUP ###
xs = [0,1,2] #x-values of the graph
ys = [4,3,2] #y-values of the graph
fig, ax = plt.subplots(1)
ax.plot(xs, ys, picker=True)
fig.canvas.mpl_connect('pick_event', onpick)
### TKINTER SETUP ###
root = tkinter.Tk()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
When this code is run and you try to click on the line, the text is never printed meaning the onpick method is never being called.
Versions
python : 3.6.1
matplotlib : 3.3.4
tkinter : 8.6.6

The event listeners of the Matplotlib Figure object cannot be called when embedded in Tkinter. Instead, you have to add the listeners to the FigureCanvasTkAgg (or similar) object. So a working example built on the previous minimal example would be:
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
def onpick(event):
print("You selected the line!")
if __name__=='__main__':
### MATPLOTLIB SETUP ###
xs = [0,1,2] #x-values of the graph
ys = [4,3,2] #y-values of the graph
fig, ax = plt.subplots(1)
ax.plot(xs, ys, picker=True)
#plt.show()
### TKINTER SETUP ###
root = tkinter.Tk()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
canvas.mpl_connect("pick_event", onpick) # Add the listener using this command
tkinter.mainloop()

Related

How to properly interrupt an animation created using matplotlib?

I would like to properly interrupt an animation.
Background:
I have a matplotlib figure (an animation) encapsulated into a tkinter instance. I want that when the user presses a tkinter button, the animation must stop, be deleted and be restarted. I am interrupting the old animation, by using del fig at the beginning of the call back function (called by the button) which deletes the old instance of the figure class and after creates a new one.
Problem:
I think that the old animations are still somehow running in the background, as I noticed that when I click the button like 5 times, the animation gets slow and moves jerkily.
Code:
import matplotlib
import matplotlib.backends.backend_tkagg as tkagg
import matplotlib.animation as animation
import numpy as np
import tkinter as tk
fig = matplotlib.figure.Figure()
ani = []
#callback function
def solve():
#I want the old animation to disappear, every time the button is pushed
global fig, ani
del fig, ani
#Creating an instance of the figure class
fig = matplotlib.figure.Figure()
#Create a Canvas containing fig into win
aCanvas =tkagg.FigureCanvasTkAgg(fig, master=win)
#Making the canvas a tkinter widget
aFigureWidget=aCanvas.get_tk_widget()
#Showing the figure into win as if it was a normal tkinter widget
aFigureWidget.grid(row=0, column=3, rowspan=10)
# create a time array
ti, tf, dt, delay=0, 10, 0.01, 15
N=(tf-ti)/dt
t = np.arange(ti, tf, dt)
x1=np.sin(t)
N=len(t)
#Creating a sub plot
ax2 = fig.add_subplot(xlim=(0, t[N-1]), ylim=(min(x1), max(x1)))
#Printing a legend with time info
time_template = 'time = %.1fs'
time_text = ax2.text(0.05, 0.95, '', transform=ax2.transAxes, bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=1'))
#I want to create a live plot for x(t). I am using place-holders where the t and x will be
massmotion, = ax2.plot([], [], '-')
CumulativeX1, CumulativeT=[], []
#I am defining what is going to go into the brackets above (I am filling the placeholders)
def animate(i):
CumulativeX1.append(x1[i]), CumulativeT.append(i*dt)
time_text.set_text(time_template % (i*dt))
massmotion.set_data(CumulativeT, CumulativeX1 ) #Update the placeholders for the live plot
return time_text, massmotion
ani = animation.FuncAnimation(fig, animate, np.arange(1, N), interval=delay, blit=True)
fig.canvas.draw()
#Creating the GUI
#Creating an instance of the Tk class
win = tk.Tk()
#Creating an instance of the Button class
#run the solve function when I click the button
aButton=tk.Button(win, text='Start Animation', command=solve)
#Placing the Button instance
aButton.grid(column=0, row=10, columnspan=2)
#Starting the event loop
win.mainloop()

Python live scatterplot in WSL with background image

I have a ROS node that subscribes to a certain topic 'first' and this returns (x,y) values, which I would like to plot on a graph with a background image. I want to be able to show only the last tuple.
I am running this python script in WSL-Ubuntu-18.04 and have installed VcXsrv in Windows to visualize the graph.
This is the code I am using but I don't know how to plot only the last (x,y) output. At the moment, all the values are plotted and after a while the plotting slows terribly down because of all the points (I guess).
from Tkinter import Canvas
import Tkinter as Tk
import rospy
from std_msgs.msg import Int32MultiArray, String
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from time import sleep
from threading import Thread
import numpy as np
from PIL import ImageTk, Image
def callback(data):
received = data.data.split(',')
x = int(float(received[0]))
y = int(float(received[1]))
plt.scatter(x,y)
def listener():
rospy.Subscriber('first', String, callback)
rospy.spin()
def animate(i):
pass
def visual():
root = Tk.Tk()
label = Tk.Label(root, text="Realtime plot ")
root.geometry("750x720")
img = plt.imread("back.jpg")
fig, ax = plt.subplots()
#mng = plt.get_current_fig_manager()
#mng.full_screen_toggle()
ax.imshow(img,extent=[0, 17947, -200, 5330], aspect='equal')
fig.canvas.draw()
plt.show()
plotcanvas = FigureCanvasTkAgg(fig, root)
plotcanvas.get_tk_widget().grid(column=0, row=0)
ani = FuncAnimation(fig, animate, interval=100, blit=False)
Tk.mainloop()
t1 = Thread(target=visual)
t1.start()
listener()
t1.join()
I'm also struggling with scaling the image (hence the whole graph) to full screen. I've tried something but only the frame gets bigger

How do I print my seaborn plot to a tkinter window, will show in IDE but doesnt stick to canvas

I have:
def create_plot():
df = pd.read_json("my_final_data.json")
small_df = df[df.small_airport.isin(['Y'])]
medium_df = df[df.medium_airport.isin(['Y'])]
large_df = df[df.large_airport.isin(['Y'])]
plt.figure(figsize=(35,10))
ax = sns.distplot(small_df['frequency_mhz'], color='red', label='Small Airports')
sns.distplot(medium_df['frequency_mhz'], color='green', ax=ax, label='Medium Airports')
sns.distplot(large_df['frequency_mhz'], ax=ax, label='Large Airports')
plt.legend(loc="upper right")
graph = plt.show()
return graph
#Generating tkinter window
window1 = tk.Tk()
figure = create_plot()
canvas = FigureCanvasTkAgg(figure, master=window1)
canvas.draw()
canvas.get_tk_widget().pack()
tk.mainloop()
Which launches 2(?) empty tkinter windows besides the canvas in one, but the IDE shows the actual graph im trying to print so I know the function is doing its job.
How do I make that returned graph stick to the window?
Youre getting 2 empty windows because you used plt.show() which is not intended to be used from within a tkinter application. The other one is an empty tkinter window (generated via tk.Tk()), without any content.
You also missed to give us sample data (my_final_data.json).
A little bit of research would have brought you a lot of examples for seaborn integration into tkinter
sns.distplot is deprecated as mentioned here, i would recommend using sns.displot or sns.histplot instead (some nice histplot examples)
This example should get you started:
import tkinter as tk
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
def create_plot(root):
# create random seaborn displot; replace this part with your own data
figure, ax = plt.subplots(figsize=(6, 6))
penguins = sns.load_dataset("penguins")
sns.histplot(data=penguins, x="flipper_length_mm", ax=ax, hue="species")
# create tkinter canvas from figure
canvas = FigureCanvasTkAgg(figure, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# optional: create toolbar
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# create your application
window1 = tk.Tk()
# call function to create plot
create_plot(window1)
# mainloop
tk.mainloop()

matplotlib PolygonSelector freezes when called in tkinter

I'm writing a script using tkinter and matplotlib for data processing, some parts of the code requires polygon selector to choose a region of interest. However, PolygonSelector fails to detect the motion of cursor.
It should be noted that this issue occurs when the interactive mode of matplotlib figure is on.
Simplified code and result are shown below:
#!/usr/bin/env python3
import matplotlib
matplotlib.use("TkAgg")
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.widgets import PolygonSelector
root = tk.Tk()
def draw():
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion() # interactive mode is on
plt.show()
def onselect(data_input):
print(data_input)
PS = PolygonSelector(ax, onselect)
tk.Button(root, text='draw', command=draw).pack()
root.mainloop()
This is the plot after clicking 'draw' button on tkinter GUI, the starting point of polygon stucks at (0,0), it is expected to move with cursor:
When I call draw() outside of tkinter, PolygonSelector works fine:
def draw():
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion() # interactive mode is on
plt.show()
def onselect(data_input):
print(data_input)
PS = PolygonSelector(ax, onselect)
a = input() # prevent window from closing when execution is done
draw()
The simple solution would be to make sure you make your Polygon Selector a global variable. This will keep the selector visually updating.
#!/usr/bin/env python3
import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import PolygonSelector
matplotlib.use("TkAgg")
root = tk.Tk()
ps = None
def draw():
global ps
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
plt.show()
ps = PolygonSelector(ax, on_select)
def on_select(data_input):
print(data_input)
tk.Button(root, text='draw', command=draw).pack()
root.mainloop()
If you build this into a class then you can avoid the use of global and get the behavior you want by apply the Polygon Selector as a class attribute.
#!/usr/bin/env python3
import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import PolygonSelector
matplotlib.use("TkAgg")
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.ps = None
tk.Button(self, text='draw', command=self.draw).pack()
def draw(self):
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
plt.show()
self.ps = PolygonSelector(ax, self.on_select)
def on_select(self, data_input):
print(data_input)
if __name__ == "__main__":
GUI().mainloop()
Results:

Using basemap as a figure in a Python GUI

I have snippets of code to generate a basemap, as well as the (very rough) start of a GUI. However, I cannot find that "one-liner" will allow me to display the map as a figure in the GUI. Code snippets are as follows; ideally, I would like a couple of things to happen:
An image of the 'basemap' to relace the plot of sin(2*pi*t)
I would really like for the code to record the (pixel) location on the graphic, if I were to click on the plot shown (the hope is that you could click anywhere on the map, and the script would record the latitude and longitude of where you clicked).
Regarding the 1st step, I've tried things such as setting the figure variable, f, equal to the Basemap; this killed the GUI portion altogether, and simply showed an image of the map in another window.
I've tried to address #2 by trying to implement a couple routines I found on stackexchange, but never got it to work fully.
Basemap:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
## User-chosen datapoint
#lons = [10]; lats = [20];
# Define map projection
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,\
llcrnrlon=-180,urcrnrlon=180,resolution='c')
m.drawcoastlines()
Rough GUI:
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from Tkinter import *
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
root = Tk.Tk()
root.wm_title("Embedding in TK")
f = Figure(figsize=(5, 4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t, s)
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def on_key_event(event):
print('you pressed %s' % event.key)
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect('key_press_event', on_key_event)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)
Tk.mainloop()
The question how to include a basemap plot into Tkinter has actually not been answered yet. So the idea is to create an axes ax in a figure and add the Basemap to the axes. This is done with the ax argument,
m = Basemap(..., ax=ax)
The figure is then added to the canvas in the usual way; below is a complete example.
from mpl_toolkits.basemap import Basemap
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
root = Tk.Tk()
root.wm_title("Embedding in TK")
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,\
llcrnrlon=-180,urcrnrlon=180,resolution='c', ax=ax)
m.drawcoastlines()
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)
Tk.mainloop()
Note: In newer versions of matplotlib you should use NavigationToolbar2Tk instead of NavigationToolbar2TkAgg.
One simple technique is to define an on_click function, show an image, then overwrite that image. The on_click function will still read the mouse click location, despite the image change.
A sample code follows, demonstrating this is as shown:
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
def on_click(event):
if event.inaxes is not None:
print event.xdata, event.ydata
else:
print 'Clicked ouside axes bounds but inside plot window'
fig, ax = plt.subplots()
fig.canvas.callbacks.connect('button_press_event', on_click)
plt.show()
#Define map projection
m = Basemap(projection='cyl', llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180, resolution='c')
m.drawcoastlines()

Categories

Resources