Using Python 3.9, matplotlib 3.4, Mac OS 11.6.1. My code below constructs a function that takes as its inputs two lists of 4-by-4 matrices having the same length. Upon a tkinter canvas I want to use a single slider to animate the corresponding sequences of heatplots on two separate sets of axes. The problem is that the slider becomes unresponsive after its value changes a couple times. What I have so far:
import numpy as np
import matplotlib.pyplot as plt
import random
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk,TOP,BOTH,Toplevel
import matplotlib
matplotlib.use('TkAgg',force=True)
def scrolling_matrix_viewer(M_list,N_list):
plot_window = Toplevel() # tkinter Toplevel upon which the figure canvas is placed.
plot_window.geometry('1000x900')
plot_window.attributes('-topmost', 'true')
fig, ax = plt.subplots(2,sharex=True)
canvas = FigureCanvasTkAgg(fig, master=plot_window) #place figure on canvas
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
fig.subplots_adjust(left=.09, bottom=.2, right=None, top=.9, wspace=.2,
hspace=.2)
Nt=len(M_list) # number of matrices in each list
ax_time=fig.add_axes([0.15, 0.1, 0.65, 0.03]) # axis for slider
spos = Slider(ax_time, '',valinit=0,valmin=0,valmax=Nt-1,valstep=1)
def update_graph(val):
t=spos.val
if t<=Nt:
ax[0].cla
ax[1].cla
heatmap0=ax[0].imshow(M_list[t],vmin=0, vmax=1, cmap='coolwarm',
aspect='auto',extent=[0,4,4,0])
heatmap1=ax[1].imshow(N_list[t],vmin=0, vmax=1, cmap='coolwarm',
aspect='auto',extent=[0,4,4,0])
spos.on_changed(update_graph)
spos.set_val(0)
return spos
Now test the function using two lists of 4-by-4 matrices
root=Tk()
M=[np.random.rand(4,4) for t in range(50)]
N=[np.random.rand(4,4) for t in range(50)]
scrolling_matrix_viewer(M,N)
root.mainloop()
The slider becomes unresponsive after I click on it twice. This is not an issue when the task is not incorporated into a function or when only one matrix sequence is used. Additionally, it is not an issue when I don’t use tkinter for purposes of embedding the figure on a canvas.