尝试使用 matplotlib 绘制大型数组时出现内存错误

发布于 2024-12-02 19:17:58 字数 538 浏览 2 评论 0原文

我想绘制一个包含 2000 万个对象的数组,我有 8GB RAM,但当我运行以下行时仍然出现以下错误:

import matplotlib.pyplot as plt
import numpy as np

d = np.arange(200000000)
plt.plot(d)
plt.show()

错误:

Traceback (most recent call last):
...
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 292, in _plot_args
    x = np.arange(y.shape[0], dtype=float)
MemoryError

I would like to plot an array of 20 millions object, I have 8GB RAM and still I get the following error when I run the following lines:

import matplotlib.pyplot as plt
import numpy as np

d = np.arange(200000000)
plt.plot(d)
plt.show()

Error:

Traceback (most recent call last):
...
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 292, in _plot_args
    x = np.arange(y.shape[0], dtype=float)
MemoryError

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满身野味 2024-12-09 19:17:58

由于显示器和绘图仪的物理限制,无论如何您都无法绘制 20,000,000 个点。因此,您可以通过采样或使用切片的方式来减少数组:

>>> m = 20000000
>>> a = np.arange(m)
>>> n = 100 # <- reducing to 100 points
>>> s = m/n # <- size of slices to compress
>>> reduced = []
>>> for i in xrange(n):
...     slice = a[i*s:(i+1)*s]
...     reduced.append(np.mean(slice))
>>> reduced
[99999.5, 299999.5, ..., 19699999.5, 19899999.5]

.. 假设 np.mean 对您正在绘制的对象有意义。

Due to physical limitations of displays and plotters, you won't be able to plot 20,000,000 points anyway. So you could reduce your array by sampling it or by using means of slices:

>>> m = 20000000
>>> a = np.arange(m)
>>> n = 100 # <- reducing to 100 points
>>> s = m/n # <- size of slices to compress
>>> reduced = []
>>> for i in xrange(n):
...     slice = a[i*s:(i+1)*s]
...     reduced.append(np.mean(slice))
>>> reduced
[99999.5, 299999.5, ..., 19699999.5, 19899999.5]

.. assuming np.mean makes sense on the objects you're plotting.

往昔成烟 2024-12-09 19:17:58

MemoryError 没有说谎——您的内存分配失败了。考虑到您试图绘制 2 亿个点(请注意,您发布的数字是 2 亿,而不是 2000 万),这并不是那么不合理。

绘制数百万个点很少或根本没有意义。当我拥有大型数据集时,我会对数据进行预处理,以便绘制不超过数千个点。对于像您这样的数据,一个简单的常规样本就可以了,但对于其他数据的良好描述可能需要峰值查找。

MemoryError isn't lying--you had a failed memory allocation. This isn't really that unreasonable considering you're trying to plot 200 million points (note that the number you posted was 200 million, not 20 million).

It seldom or never makes sense to plot millions of points. When I have large datasets, I preprocess my data so that I am plotting no more than thousands of points. A simple regular sample would be fine for this for data like you have, but peakfinding can be necessary for great depictions of other data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文