How to interactively change the animation arguments - python

I would like to interactively change a matplotlib.animation argument depending on the value provided by a GUI.
Example:
I prepared an example code which I show below, where I am trying to change the interval argument of animation based on a value provided by the user through a spinBox created with tkinter.
Problem:
In order to be able to update its argument, I want to call my animation into the call back function called by the spinbox. But if I do that, I get the following error message " UserWarning: Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation."
If I call my animation into the main code, then I won't be able to interactively change its arguments
Question:
How can I change an animation argument interactively, i.e. based on a value which the user can set in a tkinter widget?
Example code:
import tkinter as tk
from random import randint
import matplotlib as plt
import matplotlib.animation as animation
import matplotlib.backends.backend_tkagg as tkagg
#Creating an instance of the Tk class
win = tk.Tk()
#Creating an instance of the figure class
fig = plt.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=0)
#Defining the animation
ax = fig.add_subplot(xlim=(0, 1), ylim=(0, 1))
(line,) = ax.plot([],[], '-')
CumulativeX, CumulativeY = [], []
# Providing the input data for the plot for each animation step
def update(i):
CumulativeX.append(randint(0, 10) / 10)
CumulativeY.append(randint(0, 10) / 10)
return line.set_data(CumulativeX, CumulativeY)
spinBoxValue=1000
#When the button is pushed, get the value
def button():
spinBoxValue=aSpinbox.get()
#Running the animation
ani=animation.FuncAnimation(fig, update, interval=spinBoxValue, repeat=True)
#Creating an instance of the Spinbox class
aSpinbox = tk.Spinbox(master=win,from_=0, to=1000, command=button)
#Placing the button
aSpinbox .grid(row=2, column=0)
#run the GUI
win.mainloop()

We have to redraw the animation using fig.canvas.draw() when the animation is created inside the function button:
def button():
global spinBoxValue, CumulativeX, CumulativeY, ani
spinBoxValue = aSpinbox.get()
CumulativeX, CumulativeY = [], [] # This is optional
# To stop the background animation
ani.event_source.stop()
# Unlink/delete the reference to the previous animation
# del ani
ani=animation.FuncAnimation(fig, update, interval=int(spinBoxValue) * 1000, repeat=False)
fig.canvas.draw()
In the code provided, it was drawing the lines too fast when it was recreating animation using the value from aSpinbox.get(), so I changed the input to integer to draw the animation at a slower rate using interval=int(spinBoxvalue) * 1000 inside the button function.
On deleting the animation
Since we have to stop the background animation and also run the newly generated animation when the button is pressed, and because an animation must be stored in a variable as long as it runs, we will have to refer to the previous and the latest animation by the same variable name.
We can delete the animation stored in the global variable ani, using del ani after ani.event_source.stop(), which would lose the reference to the animation stored in memory before the button was pressed, but we can't really free the memory address where the reference by ani was made (I am guessing this would be true as long as we are using default garbage collection method in Python).
EDIT
Jumping to a new animation will not update/remove any variables created on the axes here - we will have to take care of it explicitly. To update variables only once after pressing the button, first create those variables in the global scope of code, and delete them inside button function and recreate/define them before/after using fig.canvas.draw:
# Defined in global scope
text = ax.text(0.7, 0.5, "text")
def button():
global spinBoxValue, CumulativeX, CumulativeY, ani, text
spinBoxValue = int(aSpinbox.get())
# To stop the background animation
ani.event_source.stop()
CumulativeX, CumulativeY = [], []
# Unlink/delete the reference to the previous animation
# del ani
text.remove()
text = ax.text(0.7 * spinBoxValue/10 , 0.5, "text")
ani=animation.FuncAnimation(fig, update, interval=spinBoxValue*1000, repeat=False)
fig.canvas.draw()
The same logic can be applied to use update function to redraw text after every button press or after every frame while using the function button provided at the very top:
text = ax.text(0.7, 0.5, "text")
# Providing the input data for the plot for each animation step
def update(i):
global text
text.remove()
# Update text after button press
# "text" is drawn at (0.7 * 1000/10, 0.5) when button = 0
text = ax.text(0.7 * spinBoxValue/10 , 0.5, "text")
# Comment previous line and uncomment next line to redraw text at every frame
# text = ax.text(0.7 * i/10 , 0.5, "text")
CumulativeX.append(randint(0, 10) / 10)
CumulativeY.append(randint(0, 10) / 10)
print(CumulativeX)
return line.set_data(CumulativeX, CumulativeY)

Related

Matplotlib mplcursors not working when figure size is Increased in tkinter canvas

I am a newbie on tkinter and python. I am trying to use mplcursors in a matplotlib figure embedded in a tkinter canvas. The figure size in the tkinter canvas can be changed as discussed on SO here. I want to add mplcursors to hover the data on the plot.
When I do not change the figure size, mplcursors works.
However, the problem occurs when the figure size is increased (to say 30 inches) mplcursors hover does not show up.
I suspect the problem is using tkinter canvas scrollbar with mplcursors. And I am not sure how to fix it.
Any suggestions or ideas, if I am missing something ?
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
import mplcursors
from matplotlib.colors import to_rgb
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)
## ADDED LINE
self.curs = mplcursors.cursor(self._axis, hover=mplcursors.HoverMode.Transient) # hover=True
# 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()

Make the program pause by button or mouse click in python package matplotlib

Through the program interface button or mouse click to make the program pause, click the button or mouse again to allow the program to continue from the place where it was last stopped, the focus is to pause how to achieve.The time from the last pause again is uncertain.My codes are following:
import matplotlib.pyplot as plt
class ab():
def __init__(self,x,y):
self.x=x
self.y=y
a=ab(2,4)
b=ab(2,6)
c=ab(2,8)
d=ab(6,10)
f=ab(6,13)
e=ab(6,15)
task=[]
task.append(a)
task.append(b)
task.append(c)
task.append(d)
task.append(e)
task.append(f)
for i in task:
while i.x<=30:
print(i.x)
plt.plot(i.x,i.y,'o')
plt.pause(0.1)
i.x=i.x+2
i.y=i.y+2
plt.show()
You can combine matplotlib's animation module with matplotlib's event connections. The code below should do more or less what you want:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# I'm replacing your class with two lists for simplicity
x = [2,2,2,6,6,6]
y = [4,6,8,10,13,15]
# Set up the figure
fig, ax = plt.subplots()
point, = ax.plot(x[0],y[0],'o')
# Make sure all your points will be shown on the axes
ax.set_xlim(0,15)
ax.set_ylim(0,15)
# This function will be used to modify the position of your point
def update(i):
point.set_xdata(x[i])
point.set_ydata(y[i])
return point
# This function will toggle pause on mouse click events
def on_click(event):
if anim.running:
anim.event_source.stop()
else:
anim.event_source.start()
anim.running ^= True
npoints = len(x)
# This creates the animation
anim = FuncAnimation(fig, update, npoints, interval=100)
anim.running=True
# Here we tell matplotlib to call on_click if the mouse is pressed
cid = fig.canvas.mpl_connect('button_press_event', on_click)
# Finally, show the figure
plt.show()

Reset Toolbar History in Matplotlib with Qt4Agg backend

I have a separate plot thread with matplotlib and multiprocesses. Now if I interactively zoom in to the window, autoscale_view() does not work anymore (fixed with using autoscale()). But the "home" button in the Toolbox is still not working: It seems to call autoscale_view() and does not show the updated view but the old view (at point when zoomed in). Example code:
import matplotlib
matplotlib.use("qt4agg")
from matplotlib import pyplot as plt
import multiprocessing
def reset_view():
plt.get
xdata = []
ydata = []
temp = 0
test_ax = plt.gca()
test_line, = plt.plot(xdata, ydata)
plt.show(block=False)
for i in range(10):
temp = temp+1
xdata.append(temp)
ydata.append(temp)
test_line.set_data(xdata, ydata)
test_ax.relim()
test_ax.autoscale(tight= False)
plt.show(block=False)
plt.pause(2)
plot_thread = multiprocessing.Process(target = reset_view, args = ())
reset_view()
if __name__ == '__main__':
plot_thread.start()
Try zooming in during plotting and pressing the Home Button after. Is there a way to either make the home button use autoscale() instead of autoscale_view() or reset & update the toolbar history, so that it doesn't jump back to old views?
P.s.: "Home"-button = reset original view
I finally was able to figure it out by trial & error. There is a function to update the toolbar. This updates the toolbar's history and sets the home() function to the new view. Solution:
figure = plt.gcf() #Get current figure
toolbar = figure.canvas.toolbar #Get the toolbar handler
toolbar.update() #Update the toolbar memory
plt.show(block = False) #Show changes

Function being called even though Matplotlib button hasn't been clicked

I have a program which shows an image (fig 1). When the image is clicked it shows the colour in the image that was clicked in a separate Matplotlib window (fig 2). Fig 2 has some buttons that call different functions when they are clicked.
My problem is that the functions that are meant to be called in fig 2 are being called when fig 1 is clicked.
The code looks like this:
def show_fig1(img):
# Plot the image
plt.figure(1)
ax = plt.gca()
fig = plt.gcf()
implot = ax.imshow(img)
# Detect a click on the image
cid = fig.canvas.mpl_connect('button_press_event', on_pixel_click)
plt.show(block=True)
# Called when fig1 is clicked
def on_pixel_click(event):
if event.xdata != None and event.ydata != None:
# Do some computation here that gets the image for fig2
img = get_fig2_img()
show_fig2(img, event)
def show_fig2(img, event):
plt.figure(2)
plt.imshow(img)
# Specify coordinates of the button
ax = plt.axes([0.0, 0.0, 0.2, 0.1])
# Add the button
button = Button(ax, 'button')
# Detect a click on the button
button.on_clicked(test())
plt.show(block=True)
def test():
print "Button clicked"
So test() is called instantly when on_pixel_click() is called even though theoretically it should wait until the button is clicked because of the button.on_clicked() command.
Any help?
Thanks in advance :)
On this line:
button.on_clicked(test())
You are telling Python to execute your test function, rather than just passing a reference to it. Remove the brackets and it should sort it:
button.on_clicked(test)

Unable to update Tkinter matplotlib graph with buttons and custom data

I'm working on creating a program that utilizes Tkinter and matplotlib. I have 2 lists of lists (one for x-axis, one for y-axis) and I'm looking to have a button that can switch between the lists within the list. I took much of the code from the question Interactive plot based on Tkinter and matplotlib, but I can't get the graph to update when the button is pressed. I'm quite new to using classes and having a bit of difficulty understanding them.
tft is the x-data tf1 is the y-data in my code.
Example of data:
x-data = [[1,2,3,4,5],[10,11,13,15,12,19],[20,25,27]]
y-data = [[5.4,6,10,11,6],[4,6,8,34,20,12],[45,25,50]]
My code below will graph one of the lists within a list, but won't switch between the lists within that list when the button is pressed. The correct value for event_num also prints in the command window (the issue of event_num was solved in a previous questions here).
There is no error that appears, the program only prints the number (when the button is pressed), but doesn't update the graph with the new data from the list.
Preliminary Code (no issues--or there shouldn't be any)
#Importing Modules
import glob
from Tkinter import *
from PIL import Image
from Text_File_breakdown import Text_File_breakdown
import re
import matplotlib.pyplot as plt
from datetime import datetime
#Initializing variables
important_imgs=[]
Image_dt=[]
building=[]
quick=[]
num=0
l=0
match=[]
#Getting the names of the image files
image_names=glob.glob("C:\Carbonite\EL_36604.02_231694\*.jpeg")
#image= Image.open(images_names[1])
#image.show()
#Text_File_breakdown(file,voltage limit,pts after lim, pts before lim)
tft,tf1,tf2=Text_File_breakdown('C:\Carbonite\EL_36604.02_231694.txt',3.0,5,5)
#tft= time of voltages tf1=Voltage signal 1 tf2=Voltage signal 2
#Test Settings: 'C:\Carbonite\EL_36604.02_231694.txt',3.0,5,5
#Getting the Dates from the image names
for m in image_names:
Idt=re.search("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}.[0-9]{2}.[0-9]{2})", m)
Im_dat_tim=Idt.group(1)
Im_dat_tim=datetime.strptime(Im_dat_tim, '%Y-%m-%d %H.%M.%S')
Image_dt.append(Im_dat_tim)
Im_dat_tim=None
#Looking for the smallest difference between the voltage and image dates and associating an index number (index of the image_names variable) with each voltage time
for event in range(len(tft)):
for i in range(len(tft[event])):
diff=[tft[event][i]-Image_dt[0]]
diff.append(tft[event][i]-Image_dt[0])
while abs(diff[l])>=abs(diff[l+1]):
l=l+1
diff.append(tft[event][i]-Image_dt[l])
match.append(l)
l=0
#Arranging the index numbers (for the image_names variable) in a list of lists like tft variable
for count in range(len(tft)):
for new in range(len(tft[count])):
quick.append(match[num])
num=num+1
building.append(quick)
quick=[]
plt.close('all')
fig, ax = plt.subplots(1)
ax.plot(tft[1],tf1[1],'.')
# rotate and align the tick labels so they look better
fig.autofmt_xdate()
# use a more precise date string for the x axis locations in the
# toolbar
import matplotlib.dates as mdates
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
plt.title('Single Event')
Continuation of code/Portion of code where the issue is:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
class App:
def __init__(self, master):
self.event_num = 1
# Create a container
frame = Frame(master)
# Create 2 buttons
self.button_left = Button(frame,text="< Previous Event",
command=self.decrease)
self.button_left.grid(row=0,column=0)
self.button_right = Button(frame,text="Next Event >",
command=self.increase)
self.button_right.grid(row=0,column=1)
fig = Figure()
ax = fig.add_subplot(111)
fig.autofmt_xdate()
import matplotlib.dates as mdates
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.canvas = FigureCanvasTkAgg(fig,master=master)
self.canvas.show()
self.canvas.get_tk_widget().grid(row=1,column=0)
frame.grid(row=0,column=0)
def decrease(self):
self.event_num -= 1
print self.event_num
self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.canvas.draw()
#self.canvas.draw(tft[self.event_num],tf1[self.event_num],'.')
#self.line.set_xdata(tft[event_num])
#self.line.set_ydata(tf1[event_num])
def increase(self):
self.event_num += 1
print self.event_num
self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.canvas.draw()
#self.canvas.draw(tft[self.event_num],tf1[self.event_num],'.')
#self.set_xdata(tft[event_num])
#self.set_ydata(tf1[event_num])
root = Tk()
app = App(root)
root.mainloop()
The problem is that you are constantly plotting on a different set of axes than you think. self.line, = ax.plot(tft[self.event_num],tf1[self.event_num],'.') refers to the axes that you created outside your class, not the axes in the figure you created in the App class. The problem can be remedied by creating self.fig and self.ax attributes:
class App:
def __init__(self, master):
self.event_num = 1
# Create a container
frame = Frame(master)
# Create 2 buttons
self.button_left = Button(frame,text="< Previous Event",
command=self.decrease)
self.button_left.grid(row=0,column=0)
self.button_right = Button(frame,text="Next Event >",
command=self.increase)
self.button_right.grid(row=0,column=1)
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.fig.autofmt_xdate()
import matplotlib.dates as mdates
self.ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
self.line, = self.ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.canvas = FigureCanvasTkAgg(self.fig,master=master)
self.canvas.show()
self.canvas.get_tk_widget().grid(row=1,column=0)
frame.grid(row=0,column=0)
def decrease(self):
self.event_num -= 1
print self.event_num
self.line, = self.ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.canvas.draw()
def increase(self):
self.event_num += 1
print self.event_num
self.line, = self.ax.plot(tft[self.event_num],tf1[self.event_num],'.')
self.canvas.draw()
Another (possible) problem is that the data gets appended to the plot instead of being replaced. There are two ways to fix this:
Turn hold off: self.ax.hold(False) somewhere in __init__() before you plot anything.
Actually replace the plot data: Replace the line
self.line, = self.ax.plot(tft[self.event_num],tf1[self.event_num],'.')
with
self.line.set_xdata(tft[self.event_num])
self.line.set_ydata(tf1[self.event_num])
You are plotting to an axes in the global namespace but redrawing the canvas on another, unrelated global object. I think your issues are about global vs. local scope and the self object in object-oriented programming.
Python scoping
Variables created in your file without indentation are in a file's global scope. class and def statements create local scopes. References to a variable name are resolved by checking scopes in this order:
Local scope
Enclosing local scope (enclosing defs only, not classes)
Global scope
Builtins
That cascade is only for references. Assignments to a variable name assign to the current global or local scope, period. A variable of that name will be created in the current scope if it doesn't already exist.
The bindings of local variables are deleted when a function returns. Values that are not bound are eventually garbage collected. If an assignment binds a global variable to a local variable's value, the value will persist with the global.
Applying this to your code
There are two sets of figure/axes objects created in your code.
# From your top chunk of code
from Tkinter import *
import matplotlib.pyplot as plt
# This line creates a global variable ax:
fig, ax = plt.subplots(1)
# From your bottom chunk of code
class App(self, master):
def __init__(self, master):
fig = Figure()
# This line creates a local variable ax:
ax = fig.add_subplot(111)
# This line references the local variable ax:
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
# This line references the local variable ax, and binds its value
# to the attribute line of the self object,
# which is the global variable app:
self.line, = ax.plot(x[self.event_n],y[self.event_n])
def increase(self):
self.event_num += 1
# This line references the global variable ax
self.line, = ax.plot(x[self.event_n],y[self.event_n])
# This line updates the object's canvas but the plot
# was made on the global axes:
self.canvas.draw()
app = App(root)
You can fix this up by always referencing an Axes associated with your Tkinter root window:
self.fig, self.ax = plt.subplots(1, 1) to create enduring Axes.
self.ax.cla() to remove the previous selection's data
self.ax.plot() to add the data at the current self.event_num index
self.ax.set_xlim(x_min*0.9, x_max*0.9) and self.ax.set_ylim(y_min*1.1, y_max*1.1) to keep the axes window consistent so as to visualize movement of the mean of data among inner lists. The x_max, y_max, x_min, and y_min can be determined before the main event loop by flattening the x and y lists and then using builtins max() and min().

Categories