使用某些投影时 Basemap.contour() 出现 IndexError
IndexError with Basemap.contour() when using certain projections
在对某些投影使用 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
请注意,当我定义 lon
和 lat
值 'properly' 时也会发生这种情况,示例只是选择尽可能短。有人知道如何解决这个问题吗?
编辑:
如果这是相关的,我在 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 此代码 returns 50 50
解决方案:
1) 为python3.
手动更新底图的contour和contourf函数
您必须为整数 n
编写:n//2
这是应用 python2.
的除法
2) 或 运行 您的程序 python2.
我找到了解决此问题的非常简单的解决方法。可以直接在 Basemap
:
的 Axes
实例上调用 contour
而不是调用 Basemap.contour
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots()
m2 = Basemap(projection='kav7',lon_0=0, ax=ax)
x = np.linspace(0, m2.urcrnrx, 100)
y = np.linspace(0, m2.urcrnry, 100)
xx, yy = np.meshgrid(x, y)
lon,lat = m2(xx,yy, inverse = True)
data = np.sin(np.pi*lon/180)*np.cos(np.pi*lat/90)
m2.drawcoastlines(linewidth=0.5)
levels = np.linspace(-1,1,8)
##m2.contour(xx, yy, data, levels)
ax.contour(xx,yy,data,levels)
plt.show()
这会在 Python 2.7 和 3.6 下生成以下图片:
此错误已 fixed 2 years ago 并且不会出现在任何 >=1.1.0 的底图版本中,与使用 python 2 或 3 无关。
在对某些投影使用 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
请注意,当我定义 lon
和 lat
值 'properly' 时也会发生这种情况,示例只是选择尽可能短。有人知道如何解决这个问题吗?
编辑:
如果这是相关的,我在 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 此代码 returns 50 50
解决方案:
1) 为python3.
手动更新底图的contour和contourf函数您必须为整数 n
编写:n//2
这是应用 python2.
2) 或 运行 您的程序 python2.
我找到了解决此问题的非常简单的解决方法。可以直接在 Basemap
:
Axes
实例上调用 contour
而不是调用 Basemap.contour
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots()
m2 = Basemap(projection='kav7',lon_0=0, ax=ax)
x = np.linspace(0, m2.urcrnrx, 100)
y = np.linspace(0, m2.urcrnry, 100)
xx, yy = np.meshgrid(x, y)
lon,lat = m2(xx,yy, inverse = True)
data = np.sin(np.pi*lon/180)*np.cos(np.pi*lat/90)
m2.drawcoastlines(linewidth=0.5)
levels = np.linspace(-1,1,8)
##m2.contour(xx, yy, data, levels)
ax.contour(xx,yy,data,levels)
plt.show()
这会在 Python 2.7 和 3.6 下生成以下图片:
此错误已 fixed 2 years ago 并且不会出现在任何 >=1.1.0 的底图版本中,与使用 python 2 或 3 无关。