Skip to content

Memory leak with log scale in pcolorfast, pcolormesh, imshow ... #15474

Closed
@ibancg

Description

@ibancg

matplotlib seems to leak memory when I switch to log scale on the Y axis, for the functions pcolorfast, pcolormesh, imshow and specgram

Please consider the following spectrogram animation (sorry for the extra dependencies to scipy)

from scipy.signal.spectral import spectrogram
from scipy.signal.waveforms import chirp

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np

fs = 8000 # sampling rate
t = np.arange(0, 5.0, 1/fs) # 5 seconds of time

fig, axes = plt.subplots(nrows=1)

def signal(t): # generates a "chirp" signal
    return chirp(t = t, f0 = 100.0,  f1 = 3000.0, t1 = 10.0)

def spect(x): # computes spectrogram
    freq, time, Sxx = spectrogram(x, fs = fs, nfft = 1024, noverlap = 512, nperseg = 1024)
    Z = 10 * np.log10(Sxx)
    return freq, time, Z

y = signal(t)
freq, time, Z = spect(y)
im = axes.pcolorfast(time, freq, Z)
axes.set_xlabel('t [s]')
axes.set_ylabel('f [Hz]')
axes.set_yscale('log') # <-- exposes the leak
axes.set_ylim(0, fs/2)

def animate(i):
    y = signal(t + i/30.0) # sliding temporal window
    freq, time, Z = spect(y)
    im.set_data(Z)
    return [ im ]

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), 
                              interval=0, blit=True)
plt.show()

Everything stays stable if I stick to linear scale on the Y axis, commenting out the line

axes.set_yscale('log')

I can reproduce it with matplotlib 3.1.1 (via pip) and previous versions (tested with 3.0.2 from system-wide Debian testing package). Also I am using Qt5Agg backend and python 3.7.5rc1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions