Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

python - 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 produces the expected result. The example uses the 'tmerc' projection.

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 declaration (commented out in the example code), the code fails with the following error:

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 and lat values 'properly', the example was only chosen to be as short as possible. Does anybody know how to resolve this?

EDIT:

In case this is relevant, I'm using python version 3.5.3 on an osx Sierra machine. The matplotlib version is 2.0.0 and the basemap version is 1.0.7 .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...