绕过"阵列太大" python错误

时间:2013-06-26 08:03:47

标签: python numpy matplotlib

我有一个csv文件,其中包含超过200 000行meteo数据。当我想使用matplotlib对数据建模时,会出现此错误:

Traceback (most recent call last):
  File "try4.py", line 19, in <module>
    X,Y = meshgrid( data_x,data_y )   
  File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 3378, in  meshgrid
    mult_fact = np.ones(shape, dtype=int)   
  File "C:\Python27\lib\site-packages\numpy\core\numeric.py", line 148, in ones
    a = empty(shape, dtype, order) 
  ValueError: array is too big.

我发现可以处理最多5000行的文件。 如何绕过错误以处理200000行的所有文件? 这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from pylab import *


# read CSV as a numpy array
data = mlab.csv2rec('datasets/mix.csv')

# print CSV file headers
print data.dtype.names

# load columns as vectors
data_x = data['longitude']
data_y = data['latitude']
data_u = data['x']
data_v = data['y']

X,Y = meshgrid( data_x,data_y )
U = cos(data_u)
V = sin(data_v)


# plot raw data
Q = quiver( X, Y, U, V, units='width')
qk = quiverkey(Q, 0.5, 0.92, 2, '.', labelpos='W',
               fontproperties={'weight': 'bold'})
title('Current Surface')

plt.show()

1 个答案:

答案 0 :(得分:2)

为什么您使用meshgriddoc)?它会生成200k乘200k的数组,与uv数据的维度不匹配。我想你想这样做

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from pylab import *


# read CSV as a numpy array
data = mlab.csv2rec('datasets/mix.csv')

# print CSV file headers
print data.dtype.names

# load columns as vectors
data_x = data['longitude']
data_y = data['latitude']
data_u = data['x']
data_v = data['y']

U = cos(data_u)
V = sin(data_v)


# plot raw data
Q = quiver(data_x, data_y, U, V, units='width')
qk = quiverkey(Q, 0.5, 0.92, 2, '.', labelpos='W',
               fontproperties={'weight': 'bold'})
title('Current Surface')