底图轮廓 - 正确指数

时间:2015-09-22 03:22:16

标签: python contour matplotlib-basemap

我正在尝试用Basemap制作等高线图。我的lat,lon和eof1数组都是1-D和79项长。当我运行此代码时,我收到一条错误消息:

IndexError: too many indices for array 

有什么建议吗?我猜一个网格网或其他东西,但我尝试的所有组合都不起作用。

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

data = np.genfromtxt('/Volumes/NO_NAME/Classwork/Lab3PCAVarimax.txt',usecols=(1,2,3,4,5,6,7),skip_header=1)

eof1 = data[:,6]

locs = np.genfromtxt('/Volumes/NO_NAME/Classwork/OK_vic_grid.txt')
lat = locs[:,1]
lon = locs[:,2]

fig, ax = plt.subplots()
m = Basemap(projection='stere',lon_0=-95,lat_0=35.,lat_ts=40,\
            llcrnrlat=33,urcrnrlat=38,\
            llcrnrlon=-103.8,urcrnrlon=-94) 

X,Y = m(lon,lat)    
m.drawcoastlines()
m.drawstates()
m.drawcountries()
m.drawmapboundary(fill_color='lightblue')
m.drawparallels(np.arange(0.,40.,2.),color='gray',dashes=[1,3],labels=[1,0,0,0])
m.drawmeridians(np.arange(0.,360.,2.),color='gray',dashes=[1,3],labels=[0,0,0,1])
m.fillcontinents(color='beige',lake_color='lightblue',zorder=0)
plt.title('Oklahoma PCA-Derived Soil Moisture Regions (Varimax)')

m.contour(X,Y,eof1)

lat和lon数据:

1   33.75   -97.75
2   33.75   -97.25
3   33.75   -96.75
4   33.75   -96.25
5   33.75   -95.75
6   33.75   -95.25
7   33.75   -94.75
8   34.25   -99.75
9   34.25   -99.25
10  34.25   -98.75
11  34.25   -98.25
12  34.25   -97.75
13  34.25   -97.25
14  34.25   -96.75
15  34.25   -96.25
16  34.25   -95.75
17  34.25   -95.25
18  34.25   -94.75
19  34.75   -99.75
20  34.75   -99.25
21  34.75   -98.75
22  34.75   -98.25
23  34.75   -97.75
24  34.75   -97.25
25  34.75   -96.75
26  34.75   -96.25
27  34.75   -95.75
28  34.75   -95.25
29  34.75   -94.75
30  35.25   -99.75
31  35.25   -99.25
32  35.25   -98.75
33  35.25   -98.25
34  35.25   -97.75
35  35.25   -97.25
36  35.25   -96.75
37  35.25   -96.25
38  35.25   -95.75
39  35.25   -95.25
40  35.25   -94.75
41  35.75   -99.75
42  35.75   -99.25
43  35.75   -98.75
44  35.75   -98.25
45  35.75   -97.75
46  35.75   -97.25
47  35.75   -96.75
48  35.75   -96.25
49  35.75   -95.75
50  35.75   -95.25
51  35.75   -94.75
52  36.25   -99.75
53  36.25   -99.25
54  36.25   -98.75
55  36.25   -98.25
56  36.25   -97.75
57  36.25   -97.25
58  36.25   -96.75
59  36.25   -96.25
60  36.25   -95.75
61  36.25   -95.25
62  36.25   -94.75
63  36.75   -102.75
64  36.75   -102.25
65  36.75   -101.75
66  36.75   -101.25
67  36.75   -100.75
68  36.75   -100.25
69  36.75   -99.75
70  36.75   -99.25
71  36.75   -98.75
72  36.75   -98.25
73  36.75   -97.75
74  36.75   -97.25
75  36.75   -96.75
76  36.75   -96.25
77  36.75   -95.75
78  36.75   -95.25
79  36.75   -94.75

eof数据

    PC5 PC3 PC2 PC6 PC7 PC4 PC1
1   0.21    0.14    0.33    0.39    0.73    0.13    0.03
2   0.19    0.17    0.42    0.24    0.78    0.1 0.04
3   0.17    0.18    0.51    0.18    0.71    0.01    0.1
4   0.18    0.2 0.58    0.19    0.67    0.07    0.11
5   0.15    0.17    0.76    0.2 0.43    0.11    0.13
6   0.12    0.16    0.82    0.17    0.34    0.12    0.15
7   0.1 0.2 0.84    0.14    0.28    0.14    0.13
8   0.16    0.09    0.2 0.73    0.29    0.25    0.1
9   0.18    0.14    0.18    0.68    0.36    0.24    0.14
10  0.23    0.22    0.18    0.63    0.53    0.21    0.14
11  0.19    0.23    0.23    0.52    0.62    0.19    0.14
12  0.2 0.18    0.23    0.43    0.74    0.15    0.11
13  0.21    0.19    0.43    0.24    0.77    0.11    0.11
14  0.15    0.21    0.51    0.15    0.72    0.1 0.15
15  0.14    0.23    0.58    0.19    0.66    0.1 0.12
16  0.13    0.22    0.74    0.19    0.49    0.13    0.12
17  0.08    0.24    0.85    0.19    0.28    0.15    0.1
18  0.1 0.29    0.86    0.15    0.18    0.16    0.07
19  0.26    0.11    0.17    0.77    0.1 0.24    0.06
20  0.36    0.16    0.14    0.74    0.24    0.23    0.12
21  0.32    0.27    0.14    0.65    0.42    0.14    0.14
22  0.39    0.29    0.21    0.58    0.47    0.09    0.21
23  0.3 0.3 0.29    0.47    0.48    0.09    0.33
24  0.25    0.35    0.35    0.42    0.45    0.09    0.45
25  0.25    0.33    0.43    0.29    0.52    0.11    0.46
26  0.24    0.36    0.48    0.26    0.53    0.09    0.4
27  0.18    0.35    0.62    0.24    0.48    0.11    0.28
28  0.13    0.4 0.83    0.12    0.15    0.12    0.06
29  0.13    0.42    0.81    0.1 0.14    0.08    0.05
30  0.45    0.14    0.14    0.7 0.05    0.2 0.04
31  0.52    0.19    0.13    0.68    0.25    0.18    0.06
32  0.53    0.2 0.16    0.66    0.32    0.09    0.08
33  0.48    0.26    0.2 0.56    0.37    0.06    0.21
34  0.41    0.34    0.28    0.44    0.35    0.06    0.43
35  0.37    0.4 0.28    0.37    0.32    0.06    0.54
36  0.24    0.41    0.39    0.27    0.33    0.11    0.56
37  0.29    0.47    0.37    0.28    0.32    0.11    0.54
38  0.3 0.61    0.36    0.25    0.26    0.13    0.47
39  0.21    0.6 0.66    0.13    0.18    0.1 0.12
40  0.13    0.48    0.75    0.1 0.13    0.07    0.06
41  0.55    0.15    0.14    0.63    0.07    0.25    0.1
42  0.55    0.19    0.17    0.65    0.13    0.2 0.11
43  0.6 0.19    0.15    0.62    0.27    0.04    0.06
44  0.63    0.18    0.16    0.53    0.25    0.04    0.16
45  0.69    0.27    0.22    0.36    0.23    -0.01   0.28
46  0.56    0.39    0.25    0.22    0.24    0.06    0.47
47  0.45    0.51    0.28    0.23    0.25    0.11    0.51
48  0.38    0.63    0.3 0.27    0.24    0.14    0.4
49  0.3 0.75    0.34    0.19    0.21    0.13    0.3
50  0.29    0.77    0.44    0.16    0.19    0.12    0.13
51  0.18    0.66    0.63    0.11    0.17    0.1 0.06
52  0.53    0.12    0.08    0.35    0.1 0.52    0.14
53  0.68    0.19    0.14    0.4 0.09    0.36    0.12
54  0.76    0.24    0.14    0.34    0.09    0.29    0.12
55  0.84    0.25    0.12    0.29    0.15    0.1 0.14
56  0.82    0.25    0.11    0.28    0.21    0.03    0.12
57  0.64    0.44    0.22    0.23    0.21    0.06    0.36
58  0.54    0.52    0.27    0.21    0.2 0.09    0.39
59  0.44    0.72    0.26    0.22    0.17    0.17    0.23
60  0.3 0.79    0.28    0.17    0.14    0.11    0.19
61  0.26    0.81    0.35    0.18    0.17    0.12    0.08
62  0.24    0.82    0.37    0.16    0.17    0.1 0.05
63  0.17    0.07    0.22    0.26    0.18    0.75    0.07
64  0.25    0.15    0.24    0.23    0.12    0.82    0.08
65  0.3 0.15    0.16    0.23    0.11    0.82    0.04
66  0.39    0.23    0.15    0.19    0.06    0.77    0.05
67  0.58    0.2 0.09    0.21    0.12    0.55    -0.1
68  0.68    0.17    0.04    0.21    0.11    0.48    -0.07
69  0.59    0.18    0.01    0.14    0.04    0.47    0.07
70  0.75    0.2 0.1 0.29    0.06    0.36    0.11
71  0.75    0.22    0.13    0.26    0.13    0.31    0.07
72  0.82    0.25    0.12    0.2 0.19    0.17    0.06
73  0.79    0.3 0.11    0.15    0.13    0.16    0.03
74  0.76    0.41    0.13    0.16    0.17    0.08    0.13
75  0.65    0.48    0.16    0.14    0.15    0.13    0.15
76  0.52    0.66    0.18    0.16    0.2 0.22    0.05
77  0.45    0.74    0.24    0.16    0.19    0.2 0.06
78  0.38    0.78    0.32    0.17    0.14    0.15    0.02
79  0.28    0.79    0.34    0.15    0.16    0.11    0

1 个答案:

答案 0 :(得分:0)

AFAICT问题的本质是你的x / y网格不是严格的矩形。 matplotlib.pyplot.contour的文档说:

  

X和Y必须都是2-D,形状与Z相同,或者两者都必须   是1-D,使得len(X)是Z和len(Y)中的列数   Z中的行数

请参阅http://matplotlib.org/api/pyplot_api.html

使用未经修改的数据,您可以通过以下方式获得箭头图:

# create vectors up and slightly right
v=eof1
u=[eof1[i]*0.5 for i in range(len(eof1))]
m.quiver(lon,lat,u,v, latlon=True)
plt.show()

因此,您必须将数据映射到轮廓()所需的1-D,1-D,2-D或2-D,2-D,2-D格式。

通过删除第1-7行和第63-68行(或者我猜你可以用0值填充它以覆盖原始区域),使数据覆盖较小的latlon矩形区域相当容易,但是到时候lon / lat被投射到你的立体投影坐标,它们不再是矩形,我认为这也是一个问题。你怎么用merc投影,只是为了让事情顺利进行?

但总的来说,我认为你需要更多的数据,特别是为了获得你的奥克拉荷马边界的轮廓,你需要数据到边界。使用latlon = True参数进行轮廓调用,以便正确转换lon和lat,即使使用merc投影也是如此。我也尝试添加参数tri = True,但这似乎对xx / y / z数据提出了不同的要求。

另一个例子,您可以使用scatter()获取气泡图:

s=[eof1[i]*500 for i in range(len(eof1))]
m.scatter(lon,lat,s=s,latlon=True)

增加:

管理得到一些轮廓!

enter image description here

最简单的解决方案是对矩形区域的lat / lon / data进行硬编码,meshgrid将1-D lon和lat转换为xx和yy中的完整2-D网格,值点为2-D。这是代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

data = np.genfromtxt('Lab3PCAVarimax.txt',usecols=(1,2,3,4,5,6,7),skip_header=1)

eof1 = data[:,6]
print eof1
eof11= [
 [  0.1   ,0.14  ,0.14  ,0.14  ,0.11  ,0.11  ,0.15  ,0.12  ,0.12  ,0.1   ,0.07]
,[  0.06  ,0.12  ,0.14  ,0.21  ,0.33  ,0.45  ,0.46  ,0.4   ,0.28  ,0.06  ,0.05]
,[  0.04  ,0.06  ,0.08  ,0.21  ,0.43  ,0.54  ,0.56  ,0.54  ,0.47  ,0.12  ,0.06]
,[  0.1   ,0.11  ,0.06  ,0.16  ,0.28  ,0.47  ,0.51  ,0.4   ,0.3   ,0.13  ,0.06]
,[  0.14  ,0.12  ,0.12  ,0.14  ,0.12  ,0.36  ,0.39  ,0.23  ,0.19  ,0.08  ,0.05]
,[  0.07  ,0.11  ,0.07  ,0.06  ,0.03  ,0.13  ,0.15  ,0.05  ,0.06  ,0.02  ,0.  ]
]
locs = np.genfromtxt('OK_vic_grid.txt')
lat = locs[:,1]
lon = locs[:,2]
lat1 = [34.25 ,34.75,35.25,35.75,36.25,36.75]
lon1 =[-99.75,-99.25, -98.75, -98.25, -97.75, -97.25, -96.75, -96.25, -95.75, -95.25, -94.75]


fig, ax = plt.subplots()
m = Basemap(projection='merc',lon_0=-95,lat_0=35.,lat_ts=40,\
            llcrnrlat=33,urcrnrlat=38,\
            llcrnrlon=-103.8,urcrnrlon=-94) 

#X,Y = m(lon,lat)    

m.drawcoastlines()
m.drawstates()
m.drawcountries()
m.drawmapboundary(fill_color='lightblue')
m.drawparallels(np.arange(0.,40.,2.),color='gray',dashes=[1,3],labels=[1,0,0,0])
m.drawmeridians(np.arange(0.,360.,2.),color='gray',dashes=[1,3],labels=[0,0,0,1])
m.fillcontinents(color='beige',lake_color='lightblue',zorder=0)
plt.title('Oklahoma PCA-Derived Soil Moisture Regions (Varimax)')

xx, yy = m(*np.meshgrid(lon1,lat1))

m.contourf(xx,yy,eof11)
plt.show()

进一步补充:实际上,当投影是立体时,这仍然有效: - )