window freezes when using specshow with plt.show()

115 views
Skip to first unread message

Oliver ulfik

unread,
May 4, 2022, 7:50:55 PM5/4/22
to librosa
I want to plot a spectrogram in a new window with plt.show() but the window freezes and the memory usage spikes up to 900 MB and more. Is there something I did miss?

import librosa
import librosa.display
import matplotlib.pyplot as plt

y, sr = librosa.load(librosa.ex('choice'), duration=15)
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
img = librosa.display.specshow(D, y_axis='linear', x_axis='time',
                               sr=sr, ax=ax[0])
ax[0].set(title='Linear-frequency power spectrogram')
ax[0].label_outer()

hop_length = 1024
D = librosa.amplitude_to_db(np.abs(librosa.stft(y, hop_length=hop_length)),
                            ref=np.max)
librosa.display.specshow(D, y_axis='log', sr=sr, hop_length=hop_length,
                         x_axis='time', ax=ax[1])
ax[1].set(title='Log-frequency power spectrogram')
ax[1].label_outer()
fig.colorbar(img, ax=ax, format="%+2.f dB")
plt.show()

Brian McFee

unread,
May 4, 2022, 8:26:40 PM5/4/22
to librosa
This all looks fine to me from the librosa side.

It might be an upstream issue, eg in matplotlib, and it might depend on what backend you're using.  Are you in interactive mode here?

The (very) recent matplotlib 3.5.2 release does include a fix for a "performance regression" in pcolormesh, which might be exactly the issue you're running into here.  Maybe try upgrading and let us know if the problem resolves itself?

Oliver ulfik

unread,
May 5, 2022, 5:28:31 AM5/5/22
to librosa
Thanks for the quick answer. Yes, you are correct. With matplotlib 3.5.2 the issue is fixed. Its still sometimes a bit slow when zooming or setting the hspace but it works. Thanks!
Reply all
Reply to author
Forward
0 new messages