Thomas Kühn - 1 month ago 11

Python Question

I have run into problems when using

`Basemap.contour`

`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`

`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`

`lat`

In case this is relevant, I'm using

`python`

`osx`

`matplotlib`

`basemap`

Answer Source

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.

For more information read this section: Integer Division Doc