IndexError with Basemap.contour() when using certain projections
basemap contourf color
fig ax basemap
python basemap inset
python basemap coordinates
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/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
lat values 'properly', the example was only chosen to be as short as possible. Does anybody know how to resolve this?
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 .
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.
python, Instead of calling Basemap.contour , one can call contour directly on the np fig,ax = plt.subplots() m2 = Basemap(projection='kav7',lon_0=0, Other fields give information about the used projection. proj4string A string that can be used with proj4 (or GDAL) to have the used projection definition; projection The code of the used projection, as indicated in the projection argument; projparams A dict with the projection parameters. The ones passed and the ones put by default by Basemap
This behavior is according to python3 integer division. Look for examples:
n=100 print (n/2, (n+1)/2)
2) For python 2.7 this code returns
1) manually update contour and contourf function of basemap with division for python3.
You have to write for integer
n//2 which is apply division from python2.
2) or run your program with python2.
Basemap utility functions, ReadAsArray() x = linspace(0, map.urcrnrx, data.shape) y = linspace(0, map.urcrnry, A colormesh and a contour fields are plotted, to be able to use some advanced The projection cyl (lat/lon), which is the default, can't use this method. basemap by matplotlib - Commit Score: This score is calculated by counting number of weeks with non-zero commits in the last 1 year period. So if 26 weeks out of the last 52 had non-zero commits and the rest had zero commits, the score would be 50%.
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
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:
basemap/__init__.py at master · matplotlib/basemap · GitHub, 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 Setting up the map¶ In order to represent the curved surface of the earth on a two-dimensional map, a map projection is needed. Since this cannot be done without distortion, there are many map projections, each with it’s own advantages and disadvantages. Basemap provides 24 different map projections.
#869156, Contribute to matplotlib/basemap development by creating an account on basemap/lib/mpl_toolkits/basemap/__init__.py projection specific parameters. axis (a) and the inverse flattening parameter using make filled contour plot. m.drawmapboundary() # draw a line around the map region. > an IndexError. Using This mean that when the original values come from a different projection, the data matrix must be re-projected, and the x and y matrices re-calculated, as you can see in the example. To calculate an evenly spaced grid, the method makegrid can be used. It’s better to use the returnxy=True attribute to get the grid in the map projection units.
matplotlib / Re: [Matplotlib-users] IndexError' too many indices' in , Package: python3-mpltoolkits.basemap; Maintainer for Maintainer, When using the "contourf" function, some map-projections (e.g. 'mill') fail with the following error condition: IndexError: only integers, slices (`:`), ellipsis (`. Basemap 버전 1.0.7, matplotlib 2.0.2가있는 Ubuntu 16.4에서 Python 3.6.1 64 비트, Qt 5.6.2, PyQt5 5.6을 사용하고 있습니다. 나는 같은 투사 'CYL'로, 베이스 맵와 contourf를 사용하려고하면 : from mpl_toolkits.basemap import Basemap import matplotlib.pyplot
fatiando.vis.mpl, I try to plot some interpolated data on a map and get an error saying > there are too many indices. When I use contour in matplotlib without > basemap I don't get the error. zi = griddata(lon,lat,var,xi,yi) > > map = Basemap(projection='cyl', llcrnrlat=67, map.drawcoastlines() > > map.drawmeridians(np.arange(0,360,1)) > Parameters: X, Y array-like, optional. The coordinates of the values in Z.. X and Y must both be 2-D with the same shape as Z (e.g. created via numpy.meshgrid), or they must both be 1-D such that len(X) == M is the number of columns in Z and len(Y) == N is the number of rows in Z.
- I cannot reproduce this error, running the code with
Basemap(projection='kav7',lon_0=0)produces this image for me.
- @ImportanceOfBeingErnest I see. Could this be an implementation problem? I'm on
osxand all the relevant packages are installed with macports.
- I have no idea. The error seems to come from within basemap. I have version 1.1.0.
- I edited the question to give some version details.
- I just checked, you are right. However, given the dates when I asked and answered the question, respectively, the problem was still valid about half a year ago. I'm guessing that, even though a bug is fixed in the code, it takes some time before the corrections find their way to all package managers...