Python 中的曲面图 - MemoryError [英] Surface Plot in Python - MemoryError

查看:77
本文介绍了Python 中的曲面图 - MemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码使用坐标创建表面图:

I'm trying to create a surface plot using coordinates using the following code:

from mpl_toolkits.mplot3d import Axes3D
from scipy.stats import gaussian_kde

fig = plt.figure()
ax = Axes3D(fig)
X = []
Y = []
XY = np.vstack([X, Y])
x, y = np.meshgrid(X, Y)
z = gaussian_kde(XY)(XY).tolist()
ax.plot_surface(x, y, z)
plt.show()

假设 X 和 Y 是浮点数的长列表.但是,当我执行代码时,我得到一个 MemoryError,它可以追溯到 meshgrid 方法:in meshgrid return [x * mult_fact for x in output].

Assume that X and Y are long lists of floating point numbers. However, when I execute the code I get a MemoryError, which traces back to the meshgrid method: in meshgrid return [x * mult_fact for x in output].

有人知道如何解决这个问题吗?

Does anyone know how to solve this?

(当我通过编写 XY = np.vstack([X [0:10],Y [0:10]])更改代码以使用列表的子集时, x,y = np.meshgrid(X [0:10],Y [0:10])曲面图有效,但有重叠之处,我想这是由于点没有按顺序绘制.任何人也知道如何解决这个问题吗?我是Python的新手...)

(When I alter the code to use a subset of the lists by writing XY = np.vstack([X[0:10], Y[0:10]]) and x, y = np.meshgrid(X[0:10], Y[0:10]) the surface plot works, but overlaps. I suppose this is due to the points not being plotted in order. Does anyone know how to resolve this also? I am new to Python...)

推荐答案

您试图绘制太多点.要查看此示例,请提供一些假数据:

You are trying to plot too many points. To see this give your example some fake data:

from mpl_toolkits.mplot3d import Axes3D
from scipy.stats import gaussian_kde
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

npoints = 100
X = np.linspace(0, 1, npoints)
Y = np.sin(X)
XY = np.vstack([X, Y])
x, y = np.meshgrid(X, Y)
z = gaussian_kde(XY)(XY).tolist()
ax.plot_surface(x, y, z)
plt.show()

这可以正常工作,但是如果我们使 npoints = 10000 ,那么(在我的机器上)我可以重现您的错误.

This will work just fine, but if we make npoints=10000 then (on my machine) I can reproduce your error.

解决方案完全取决于 XY 是什么.例如,如果它们是一些测量数据输出,那么您需要问我是否需要绘制所有这些数据?"的问题.根据我的经验,如果您尝试绘制如此多的点以致出现内存错误,那么无论如何绘制时您将无法看到所有数据!所以,你可以对它进行采样,只需选择每个 nth 点.首先,我建议将 X Y 转换为 numpy 数组

The solution depends on exactly what X and Y are. For example if they are some measured data output then you need to ask the question "do I need to plot all of this data?". In my experience if your trying to plot so many points that you get a memory error, then you won't be able to see all of the data when it is plotted anyway! So, you could down sample it, just pick every nth point. First I would suggest turning X and Y into numpy arrays

import numpy as np
X = np.array(X)
Y = np.array(Y)

然后,要获得每100个积分,请执行以下操作:

Then, to get every 100th point do:

X = X[::100]
Y = Y[::100]

有关数组切片和索引的更多信息请参阅文档.

For more information on array slicing and indexing see the docs.

或者,您可能会觉得这丢失"了一些有价值的信息,在这种情况下,您可以应用滚动窗口平均值,然后单独考虑每个窗口中的变化,以检查您没有遗漏任何内容.最终这一切都取决于数据是什么.

Alternatively you may feel that this "looses" some valuable information, in which case you could apply a rolling window average then separately consider the variations in each window to check you haven't missed anything. Ultimately it will all depend on what the data is.

这篇关于Python 中的曲面图 - MemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆