-->

IndexError when using some projections with basema

2019-07-26 03:31发布

问题:

I'm using Python 3.6.1 64bits, Qt 5.6.2, PyQt5 5.6 on Ubuntu 16.4 with Basemap version 1.0.7, matplotlib 2.0.2. When I try to use a contourf with basemap, with projection 'cyl', such as:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

map = Basemap(projection='cyl',lat_0=45,lon_0=-100,resolution='c')

nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
lons = (delta*np.indices((nlats,nlons))[1,:,:])
wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)

x, y = map(lons*180./np.pi, lats*180./np.pi)

cs = map.contour(x,y,wave+mean,15,linewidths=1.5)
plt.show()

I get the following error:

Traceback (most recent call last):

File "", line 15, in cs = map.contour(x,y,wave+mean,15,linewidths=1.5)

File "/home/gab/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/init.py", line 521, in with_transform return plotfunc(self,x,y,data,*args,**kwargs)

File "/home/gab/anaconda3/lib/python3.6/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

This error does not happen if I use for instance the projection 'ortho'. I have no problem running this code on Python 2.6. This question seems to be the same as this one, which was not answered.

Any ideas?

回答1:

In response to lanadaquenada (I cannot coment it seems)

Based on Serenity's post you actually need to modify the basemap code, rather than your code. Basemap is old and not really supported anymore. It was created when python 2.x was released and it appears it utilised the python 2 integer division. Python 3 now does division "correctly", but some old code was created to take advantage of python 2 division.

While using python3 and matplotlib 1.5.3 I would receive a warning about this issue, but it was not fatal. After upgrading to matplotlib 2.0.2 this error became fatal and my googling lead to your post.

Therefore following Serenity's advice you need to Manually change lines with

xx[x.shape[0]/2, :]

to

xx[x.shape[0]//2, :] 

For me this was 3452 and 3644 in path_where_your_python_libraries_are_installed/site-packages/mpl_toolkits/basemap/__init__.py

I am using basemap version 1.0.7.

I needed to make this change when transitioning from matplotlib 1.5.3 to version 2.0.2

This stopped my code from crashing and my basic testing against older matplotlib version appears to produce the correct results.

I hope this does not have unintended consequences else where, though basemap was designed with the old integer division so I assume it is OK



回答2:

This behavior is the sequent of 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 lines 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.