0

I have a sequence of 50, 6-by-6 matrices and wish to create an animation (with player) and then place this animation and player on a tkinter figure canvas.

For placing the heatmap of a single matrix, M, on a tkinter canvas, my usual approach is as follows:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk,TOP,BOTH

M=np.random.rand(6,6)

root=Tk()
root.geometry('1000x1000')

fig,ax=plt.subplots()
ax.imshow(M,aspect='auto',cmap='jet')

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)

root.mainloop()

Now I made a simple change to the approach described at

Managing dynamic plotting in matplotlib Animation module

to create a matplotlib animation of heatplots for my list of matrices, labelled M_list:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import mpl_toolkits.axes_grid1
import matplotlib.widgets

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import Tk,TOP,BOTH

root=Tk()
root.geometry('1000x1000')

class Player(FuncAnimation):
    def __init__(self, fig, func, frames=None, init_func=None, 
    fargs=None,save_count=None, mini=0, maxi=100, pos=(0.125, 0.92), **kwargs):
    self.i = 0
    self.min=mini
    self.max=maxi
    self.runs = True
    self.forwards = True
    self.fig = fig
    self.func = func
    self.setup(pos)
    FuncAnimation.__init__(self,self.fig, self.func, frames=self.play(),
                                       init_func=init_func, fargs=fargs,
                                       save_count=save_count, **kwargs )

    def play(self):
        while self.runs:
            self.i = self.i+self.forwards-(not self.forwards)
            if self.i > self.min and self.i < self.max:
                yield self.i
            else:
                self.stop()
                yield self.i

    def start(self):
        self.runs=True
        self.event_source.start()

    def stop(self, event=None):
        self.runs = False
        self.event_source.stop()

    def forward(self, event=None):
        self.forwards = True
        self.start()
    def backward(self, event=None):
        self.forwards = False
        self.start()
    def oneforward(self, event=None):
        self.forwards = True
        self.onestep()
    def onebackward(self, event=None):
        self.forwards = False
        self.onestep()

    def onestep(self):
        if self.i > self.min and self.i < self.max:
            self.i = self.i+self.forwards-(not self.forwards)
        elif self.i == self.min and self.forwards:
            self.i+=1
        elif self.i == self.max and not self.forwards:
            self.i-=1
        self.func(self.i)
        self.fig.canvas.draw_idle()

    def setup(self, pos):
        playerax = self.fig.add_axes([pos[0],pos[1], 0.22, 0.04])
        divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax)
        bax = divider.append_axes("right", size="80%", pad=0.05)
        sax = divider.append_axes("right", size="80%", pad=0.05)
        fax = divider.append_axes("right", size="80%", pad=0.05)
        ofax = divider.append_axes("right", size="100%", pad=0.05)
        self.button_oneback = matplotlib.widgets.Button(playerax , label=u'$\u29CF$')
        self.button_back = matplotlib.widgets.Button(bax, label=u'$\u25C0$')
        self.button_stop = matplotlib.widgets.Button(sax, label=u'$\u25A0$')
        self.button_forward = matplotlib.widgets.Button(fax, label=u'$\u25B6$')
        self.button_oneforward = matplotlib.widgets.Button(ofax, label=u'$\u29D0$')
        self.button_oneback.on_clicked(self.onebackward)
        self.button_back.on_clicked(self.backward)
        self.button_stop.on_clicked(self.stop)
        self.button_forward.on_clicked(self.forward)
        self.button_oneforward.on_clicked(self.oneforward)


fig, ax = plt.subplots()
num_times=50
M_list=[]
for t in range(num_times):
    M_list.append(np.random.rand(6,6))

def update(i):
    ax.imshow(M_list[i],aspect='auto',cmap='jet')

ani = Player(fig, update, maxi=num_times)

This works fine in terms animating the sequence of heatmaps stored in M_list (in a way that permits advancing a single frame at a time).

Now I'm stuck trying to place the matplotlib animation on my figure canvas. I tried placing the long block of code inside a tkinter root window:

root=Tk()
root.geometry

Use fig as defined in animation above.

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)

root.mainloop() 

This produced the error message:

Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL 
held, but the GIL is released (the current Python thread state is NULL)

Using Python 3.9 on a Mac with Big Sur.

fishbacp
  • 865
  • 8
  • 20

0 Answers0