如何在绘制3个变量的函数时使用非常大的数组来处理MemoryError消息?

时间:2016-01-27 02:52:59

标签: python arrays python-3.x numpy matplotlib

假设F(x1,x2,x3)是三个独立变量x1,x2和x3的函数,它们以1D阵列的形式给出。我想将F(利用轮廓类)绘制为x3的几个值。下面是我的代码片段(我假设它是由错误产生的地方,除非另有说明)包括:

N = 10000
data1 = [random.uniform(-1, 1) for i in range(N)]
vx = ar(data1)
data2 = [random.uniform(-1, 1) for i in range(N)]
vy = ar(data2)
data2 = [random.uniform(-1, 1) for i in range(N)]
vz = ar(data2)

def P(vx, vy, vz):
        y1 = .... # some function of vx
        y2 = .... # some function of vy
        y3 = .... # some function of vz
        return y1 * y2 * y3

X, Y = np.meshgrid(vx, vy)
vz_vals = [-1, -0.5, -0.1, 0, 0.1, 0.5, 1]
num_subplots = len(vz_vals)

fig = pyplot.figure(figsize=(10, 4))
for i,vz in enumerate(vz_vals) :
    ax = fig.add_subplot(1, num_subplots, i + 1, projection='3d', axisbg='gray')
    ax.contourf(X, Y, P(X, Y, vz), cmap = cm.gnuplot)
    ax.set_title('vz = %.2f' % vz, fontsize = 30)
fig.savefig('myfigure.png', facecolor='grey', edgecolor='none')

当我将代码更改为以下内容时,问题会解决,但现在我遇到“内存”问题,我不知道如何处理它,因为我不允许发布新问题我希望有人会看到问题。感谢您的所有关注,

N = 10000
vx = [random.uniform(-1, 1) for i in range(N)]
vy = [random.uniform(-1, 1) for i in range(N)]
vz = [random.uniform(-1, 1) for i in range(N)]

def P(vx, vy, vz):
        y1 = .... # some function of vx
        y2 = .... # some function of vy
        y3 = .... # some function of vz
        return y1 * y2 * y3

X, Y = np.meshgrid(vx, vy)
vz_vals = [-1, -0.5, -0.1, 0, 0.1, 0.5, 1]
num_subplots = len(vz_vals)

fig = pyplot.figure(figsize = (10, 4))
for i, vz in enumerate(vz_vals):
      ax = fig.add_subplot(1, num_subplots, i+1, projection = '3d', axisbg = 'gray')
      ax.contour(X, Y, P(X, Y, vz), camp = cm.gnuplot)
      ax.set_title('vz = %.2f'% vz, fontsize = 30)
fig.savefig('myfigure.png', facecolor = 'grey', edgecolor = 'none')

新错误消息:

  

Traceback(最近一次调用最后一次):文件“example.py”,第11行,in       ax.contour(X,Y,P(X,Y,vz),camp = cm.gnuplot)文件“example.py”,第2行,在P中       gauss1 = .... MemoryError

1 个答案:

答案 0 :(得分:0)

要拨打np.meshgrid[x1, x2],您需要使用np.meshgrid(x1, x2)

等圆括号