尝试使用 matplotlib 绘制大型数组时出现内存错误
我想绘制一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于显示器和绘图仪的物理限制,无论如何您都无法绘制 20,000,000 个点。因此,您可以通过采样或使用切片的方式来减少数组:
.. 假设 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:
.. assuming
np.mean
makes sense on the objects you're plotting.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.