IndexError with Basemap.contour() when using certain projections

烈酒焚心 提交于 2019-11-27 19:38:15

问题


I have run into problems when using Basemap.contour with certain projections. Based on the example given in the Basemap documentation, I created the following working code which produces the expected result. The example uses the 'tmerc' projection.

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()

However, if I switch to using the 'kav7' projection in the alternative m2=Basemap declaration (commented out in the example code), the code fails with the following error:

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

Note that this also happens when I define lon and lat values 'properly', the example was only chosen to be as short as possible. Does anybody know how to resolve this?

EDIT:

In case this is relevant, I'm using python version 3.5.3 on an osx Sierra machine. The matplotlib version is 2.0.0 and the basemap version is 1.0.7 .


回答1:


This bug has been fixed 2 years ago and does not occur in any basemap version >=1.1.0, independent of the use of python 2 or 3.




回答2:


This behavior is according to python3 integer division. Look for examples:

1) python3:

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

Output: 50.0 50.5

2) For python 2.7 this code returns 50 50

Solutions:

1) manually update contour and contourf function of basemap with division for python3.

You have to write for integer n: n//2 which is apply division from python2.

2) or run your program with python2.




回答3:


I found a really simple workaround to this problem. Instead of calling Basemap.contour, one can call contour directly on the Axes instance of the Basemap:

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()

This produces the following picture both under Python 2.7 and 3.6:



来源:https://stackoverflow.com/questions/44519804/indexerror-with-basemap-contour-when-using-certain-projections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!