I have made an animated spectrogram this way:
import matplotlib.pyplot as plt
from matplotlib import animation
import librosa
window = 100_000
jump = 1000
interval = 1
filename = 'sound.wav'
sound, rate = librosa.load(filename, sr=None)
fig = plt.figure()
def animate(i):
chunk = sound[i * jump: i * jump + window]
_, _, _, im = plt.specgram(chunk, Fs=rate)
return im,
ani = animation.FuncAnimation(fig, animate, interval=interval, blit=True)
plt.ion()
plt.show()
It works, but in examples of using FuncAnimation I've seen, people don't call the whole plotting function for every animation frame but update the data directly instead and it feels as if there are probably reasons (performance?) to do this. The examples gave some idea of how to do this for other images (ones where you were basically doing your own math to fill the array that is the image) but with something a little more blackbox-ish like pyplot.specgram my (maybe) hack was to just call the plotting function over and over. My question is can this be done in a way more like the example in the link above on the matplotlib site and, if yes, how?