data_jay
data_jay

Reputation: 73

Using Python basemap etopo() generates IndexError

I'm trying to basemap working. I've installed matplotlib, basemap, and Pillow successfully and can plot basic plots. The code below works for me.

from mpl_toolkits.basemap import Basemap
from PIL import Image
import matplotlib.pyplot as plt

m = Basemap(projection = 'mill',
        llcrnrlat = -90,
        llcrnrlon = -180,
        urcrnrlat = 90,
        urcrnrlon = 180,
        resolution = 'c')

m.drawcoastlines()
#m.etopo()

plt.show()

However, uncommenting the 'etopo()' command yields the following error.

Traceback (most recent call last):

  File "C:\Users\xxx\Desktop\helper.py", line 13, in <module> m.etopo()

  File "C:\Python35-32\lib\site-packages\mpl_toolkits\basemap\__init__.py",    line 4061, in etopo return self.warpimage(image='etopo',scale=scale,**kwargs)

  File "C:\Python35-32\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 4167, in warpimage np.concatenate((self._bm_lons,self._bm_lons+360),1)

IndexError: axis 1 out of bounds [0, 1)

I've tried reinstalling and updating all relevant packages, but that hasn't worked. I also can't find anything on this error for this situation. I'm on Windows 8.1 with 32 bit Python 3.5.1

Upvotes: 0

Views: 1374

Answers (2)

Benjamin R
Benjamin R

Reputation: 21

This seems to be an incompatibilty between basemap and recent numpy versions. The suggested solution reverts to the default projection, since the first keyword is not projection but llcrnrlat.

I haven't figures out what exactly causes the IndexError, however reverting to an older numpy version works. It seems numpy 1.9.2 is the latest working version. It might be that something was changed with the concatenate command.

Edit: I changed line 4167 in basemap/init.py to obtain the same result than it must have been with older numpy versions. It seems to do the trick:

Old: np.concatenate((self._bm_lons,self._bm_lons+360),1)
New: np.concatenate((self._bm_lons.T,self._bm_lons.T+360)).T

Concatenating along 1D arrays and a non zero axis, is now deprecated, which is why the exception is raised.

Upvotes: 2

fslick
fslick

Reputation: 46

If you are just testing Basemap and .etopo() functionality, I updated your script:

from mpl_toolkits.basemap import Basemap
from PIL import Image
import matplotlib.pyplot as plt

# make a miller cylindrical projection with defaults
m = Basemap('mill')

m.drawcoastlines()
# now displays topo relief image
m.etopo()

plt.show()

Upvotes: 3

Related Questions