Python Basemap: Error using shadedrelief, bluemarb

2019-03-02 02:38发布

问题:

I want to plot a map of the southern hemisphere centered on the pacific with some stuff drawn onto it with python matplotlib basemap.

Everything works fine unless I try to draw a background image with the basemap routines shadedrelief, bluemarble or etopo. The code (without the stuff i want to draw onto the map) looks like this:

import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib.backends.backend_pdf import PdfPages

latmin = -72.5
latmax = 40.
lonmin = 60.
lonmax = 370.

pp = PdfPages('datamap.pdf')

m = Basemap(projection='merc', llcrnrlat=latmin, urcrnrlat=latmax, llcrnrlon=lonmin, urcrnrlon=lonmax, resolution="c")

m.drawcoastlines(linewidth=0.25)
#m.shadedrelief()

pp.savefig()
pp.close()

When I uncomment the m.shadedrelief() I get the following:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/xyz/datamap.py in <module>()
     32
---> 33 m.shadedrelief()
     34 

/usr/local/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc in shadedrelief(self, ax, scale, **kwargs)
   3997             return self.warpimage(image='shadedrelief',ax=ax,scale=scale,**kwargs)
   3998         else:
-> 3999             return self.warpimage(image='shadedrelief',scale=scale,**kwargs)
   4000 
   4001     def etopo(self,ax=None,scale=None,**kwargs):

/usr/local/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.pyc in warpimage(self, image, scale, **kwargs)
   4115                 # any range of longitudes may be plotted on a world map.
   4116                 self._bm_lons = \
-> 4117                 np.concatenate((self._bm_lons,self._bm_lons+360),1)
   4118                 self._bm_rgba = \
   4119                 np.concatenate((self._bm_rgba,self._bm_rgba),1)
IndexError: axis 1 out of bounds [0, 1)

When I choose (for test purposes) way smaller maps which also dont have longitude larger than 180 degree everything (including shadedrelief) works fine. This leads me to the assumption that something with the format of the longitude is not working here. I tried some things but i cant seem to find a way how to solve this while still plotting the same map section

Do you have any idea how i could draw a shadedrelief in the background of my map?

Best,

xilian

回答1:

It seems there was a bug in the basemap/init code. In fact, if you look at the latest version on github [see https://github.com/matplotlib/basemap/blob/master/lib/mpl_toolkits/basemap/init.py#L4139 and how it is different from line 4117 of your error] you will find out that the bug has been solved already. This means you can either do:

# np.concatenate((self._bm_lons,self._bm_lons+360),1)
np.concatenate((self._bm_lons,self._bm_lons+360))

in the init.py code or get a newer version of basemap. Any of the two will solve your problem.