Skip to content Skip to sidebar Skip to footer

Indexerror With Basemap.contour() When Using Certain Projections

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 pr

Solution 1:

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.

Solution 2:

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:

result of the above code

Solution 3:

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.

Post a Comment for "Indexerror With Basemap.contour() When Using Certain Projections"