IndexError with Basemap.contour() when using certain projections

心已入冬 提交于 2019-11-28 14:24:52

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.

Serenity

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.

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:

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