gpt4 book ai didi

python - 使用某些投影时 Basemap.contour() 出现 IndexError

转载 作者:行者123 更新时间:2023-12-01 02:53:00 25 4
gpt4 key购买 nike

我在使用 Basemap.contour 进行某些投影时遇到了问题。基于给出的示例 in the Basemap documentation ,我创建了以下工作代码,它产生了预期的结果。该示例使用“tmerc”投影。

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


m2 = Basemap(projection='tmerc',
lat_0=0, lon_0=3,
llcrnrlon=1.819757266426611,
llcrnrlat=41.583851612359275,
urcrnrlon=1.841589961763497,
urcrnrlat=41.598674173123)
##m2 = Basemap(projection='kav7',lon_0=0)

x = np.linspace(0, m2.urcrnrx, 100)
y = np.linspace(0, m2.urcrnry, 100)
xx, yy = np.meshgrid(x, y)
data = np.sin(xx/100)*np.cos(yy/100)

levels = np.linspace(-1,1,8)
m2.contour(xx, yy, data, levels)

plt.show()

但是,如果我在替代 m2=Basemap 声明中切换到使用“kav7”投影(在示例代码中注释掉),则代码会失败并出现以下错误:

Traceback (most recent call last):
File "basemap_contour.py", line 20, in <module>
m2.contour(xx, yy, data, levels)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py", line 521, in with_transform
return plotfunc(self,x,y,data,*args,**kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py", line 3542, in contour
xx = x[x.shape[0]/2,:]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

请注意,当我“正确”定义 lonlat 值时也会发生这种情况,该示例仅选择尽可能短。有谁知道如何解决这个问题吗?

编辑:

如果这是相关的,我在 osx Sierra 机器上使用 python 版本 3.5.3。 matplotlib 版本为 2.0.0 , basemap 版本为 1.0.7 。

最佳答案

此行为根据 python3 整数除法。寻找示例:

1) python3:

n=100
print (n/2, (n+1)/2)

输出:50.0 50.5

2) 对于 python 2.7,此代码返回 50 50

解决方案:

1)手动更新 basemap 的contour和contourf函数,并为python3划分。

对于整数,您必须编写n:n//2,这是从python2应用除法。

2) 或使用 python2 运行您的程序。

关于python - 使用某些投影时 Basemap.contour() 出现 IndexError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44519804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com