Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory leak in matplotlib histogram

Running the following code will result in memory usage rapidly creeping up.

import numpy as np
import pylab as p
mu, sigma = 100, 15
x = mu + sigma*np.random.randn(100000)
for i in range(100):
    n, bins, patches = p.hist(x, 5000)

However, when substituting the call to pylab with a direct call to the numpy histogram method then memory usage is constant (it also runs significantly faster).

import numpy as np
mu, sigma = 100, 15
x = mu + sigma*np.random.randn(100000)
for i in range(100):
    n, bins = np.histogram(x, 5000)

I was under the impression that pylab is using the numpy histogram function. There must be a bug somewhere...

like image 884
schubnel Avatar asked Mar 04 '26 05:03

schubnel


1 Answers

Matplotlib generates a diagram. NumPy does not. Add p.show() to your first code to see where the work goes.

import numpy as np
import pylab as p
mu, sigma = 100, 15
x = mu + sigma*np.random.randn(100000)
n, bins, patches = p.hist(x, 5000)
p.show()

You may want to try with a smaller number for np.random.randn(100000) first to see something quickly.

EDIT

Does not really make sense to create the same plot 100 times.

like image 78
Mike Müller Avatar answered Mar 05 '26 19:03

Mike Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!