pyplot hist中的内存错误

时间:2017-06-02 08:41:57

标签: python python-2.7 matplotlib

我的列表大小为1247746130.我想获得此列表的直方图:

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

但是我收到了错误:

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

我有8GB内存。是否有可能获得我的数据的直方图?

提前致谢!

1 个答案:

答案 0 :(得分:1)

我过去曾遇到过plt.hist的问题所以现在使用numpy.histogram。 (虽然我认为plt.hist确实在幕后使用numpy.histogram。示例如下所示:

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()