我收到了错误:
TypeError: 'float' object cannot be interpreted as an integer
当我致电maskoceans
时。为什么是这样?我正在运行python3。
from mpl_toolkits.basemap import Basemap
from mpl_toolkits.basemap import maskoceans
from mpl_toolkits.basemap import interp
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import numpy as np
places = {
(-8.91508040128176, 52.23826465): 0,
(-6.45854802816101, 52.64127685): 21,
(-9.03867527891856, 52.78809005): 0,
(-8.70926037608263, 53.717247): 0,
(-6.8217677, 54.3303964): 0,
(-6.1658125, 53.5255827): 4
}
m = Basemap(llcrnrlon=-10.56, llcrnrlat=51.39, urcrnrlon=-5.34, urcrnrlat=55.43,
lat_1=30., lat_2=60., lat_0=53.41, lon_0=-7.95,
resolution="i")
x, y, values = np.array([(x, y, v) for (x, y), v in places.items()]).T
coords = np.stack((x, y)).T
x, y = np.mgrid[-11:-4:10j, 51:56:10j]
z = griddata(coords, values, (x, y), method='nearest')
mdata = maskoceans(x, y, z, resolution = 'h', grid = 10, inlands=True)
plt.contour(x[:,0], x[0,:], mdata.T,linewidths=0.5,colors='k',z=99)
plt.contourf(x[:,0], y[0,:], mdata.T,cmap=plt.cm.Purples, z=100)
这是完整的堆栈跟踪:
Traceback (most recent call last):
File "plot.py", line 28, in <module>
mdata = maskoceans(x, y, z, resolution = 'h', grid = 10, inlands=True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py", line 5107, in maskoceans
_readlsmask(lakes=inlands,resolution=resolution,grid=grid)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mpl_toolkits/basemap/__init__.py", line 5132, in _readlsmask
np.reshape(np.fromstring(lsmaskf.read(),dtype=np.uint8),(nlats,nlons))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/core/fromnumeric.py", line 257, in reshape
return _wrapfunc(a, 'reshape', newshape, order=order)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/core/fromnumeric.py", line 62, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/core/fromnumeric.py", line 42, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: 'float' object cannot be interpreted as an integer
答案 0 :(得分:1)
如果您使用Python3,则需要对此文件进行简单的更改:
“your_python3_install_directory / LIB / python3.x /站点包/ mpl_toolkits /底图/的初始化强> py” 为
在第5125行和第5130行之间,找到“nlats = nlons / 2”并将其更改为“nlats = nlons // 2”。
这应该可以解决问题。