I’m making a website in which users can input one NBA player and get an animation of their shots. The code is working properly, but when I load another NBA player (or the same NBA player the second time), the Matplotlib animation works more slowly.
For example, if I go to the URL 127.0.0.1:5000/?nbaplayer=playerA
, the animation will take 20 seconds to load, but then if I go to URL http://127.0.0.1:5000/?nbaplayer=playerA
again, the animation will take 30 seconds. Another time, it will take 40 seconds, and so on.
Specifically, the line.set_data_3d()
takes up more time than before, which I have timed. I’ve attached the relevant code:
from flask import Blueprint, render_template, request
from website.animator import *
matplotlib.use('agg')
views = Blueprint('views', __name__)
@views.route('/', methods = ['GET', 'POST'])
def home():
args = request.args
if len(args) == 2:
...
walks = [] #walks will be a 100 x 3 array
for x,y in zip(player_shotchart['LOC_X'], player_shotchart['LOC_Y']):
walks.append(getParabola(-y, -x))
lines = [ax.plot([], [], [])[0] for _ in walks]
num_steps = 30 * int(np.log(len(walks))) + 1
#The problem here is that ani loads more slowly when reloading
ani = animation.FuncAnimation(
fig, update_lines, num_steps, fargs=(walks, lines), interval=30, repeat = False)
ani_saved = ani.to_jshtml()
return render_template("base2.html", playerName = args['nbaplayer'], playerSZN = args['nbaseason'], rendered_anim = ani_saved)
And, here’s my update_lines
function. For context, walks
is an 100 x 3 array.
def update_lines(num, walks, lines):
for line, walk in zip(lines, walks):
line.set_data_3d(walk[num:num+3, :].T)
return lines
I’ve tried using fig.clf()
and fig.cla()
in order to clear the axes and completely refresh the figure. Additionally, I’ve also tried clearing each of the lines by doing line.set_data_3d(np.array([]), np.array([]), np.array([]))
, in another animation, update_lines2
.
However, none of these seem to make a difference, as the individual times of line.set_data_3d
seems to keep increasing. I’ve even tried clearing cookies and site data, but it doesn’t work.