user2080209
user2080209

Reputation: 779

Memory error in pyplot hist

I have a list size of 1247746130. I want to get a histogram for this list:

bins = np.linspace(-100, 1, 100)
plt.hist(refList, bins, alpha=0.5, label='reference')
plt.legend(loc='upper right')
plt.savefig('reference.png')

but I get an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3081, in hist
    stacked=stacked, data=data, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1898, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 6146, in hist
    x = _normalize_input(x, 'x')
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 6083, in _normalize_input
    inp = np.asarray(inp)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
MemoryError

I have 8GB RAM. Is it somehow possible to get a histogram for my data?

Thanks in advance!

Upvotes: 1

Views: 1574

Answers (1)

DavidG
DavidG

Reputation: 25371

I've had problems with plt.hist in the past so now use numpy.histogram. (although I think plt.hist does actually use numpy.histogram behind the scenes). An example is shown below:

import numpy as np
import matplotlib.pyplot as plt

bins = np.linspace(-100, 1, 100)

heights, edges = np.histogram(data, bins)
edges = edges[:-1]+(edges[1]-edges[0])

fig, ax = plt.subplots()
ax.plot(edges, heights)
plt.show()

Upvotes: 2

Related Questions