using scalebar in matplotlib basemap
Asked Answered
K

1

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

m = Basemap(projection='cyl',resolution='c',area_thresh=10,llcrnrlon=-180,urcrnrlon=180,\
    llcrnrlat=-90,urcrnrlat=90)
m.etopo()

Actually, I did not know how to provide the lat, lon, lat0, and lon0 parameters required to show scale-bar. How to provide them?

map.drawmapscale(????,barstyle='simple',units='km',fontsize=9,labelstyle='simple',fontcolor='k')

The tutorial at

http://matplotlib.org/basemap/api/basemap_api.html describe it as follows:

drawmapscale(lon, lat, lon0, lat0, length, barstyle='simple', units='km', fontsize=9, yoffset=None, labelstyle='simple', fontcolor='k', fillcolor1='w', fillcolor2='k', ax=None, format='%d', zorder=None)

Would appreciate if someone could help me.

Kedge answered 12/12, 2013 at 16:1 Comment(3)
If you would provide the right values you will run into: ValueError: cannot draw map scale for projection='cyl'. You cant make a scale in kilometers for a map in degrees, the length of a km is different at every location.Kyanize
@ Rutger Kassies ok then can you show me an example of drawing scalebar using degree as the unit?Kedge
Adding gridlines via drawmeridians and drawparallels will likely be more helpful than a scale bar for degrees.Secede
S
6

It appears that drawmapscale doesn't support Basemap instances with projection='cyl' (and possibly others; I have only checked projection='cyl' and projection='moll'):

In [7]: m = Basemap(projection='cyl',resolution='c',area_thresh=10,llcrnrlon=-180,\
                    urcrnrlon=180, llcrnrlat=-90,urcrnrlat=90)

In [8]: m.etopo()
Out[8]: <matplotlib.image.AxesImage at 0x10a899e90>

In [10]: m.drawmapscale(50, -75, 0, 0, 400)

This results in the following error:

ValueError: cannot draw map scale for projection='cyl'

But drawmapscale does appear to work for other projections. Using Mollweide, for example:

In [11]: m = Basemap(projection='moll', lon_0=0)
In [12]: m.etopo()
Out[12]: <matplotlib.image.AxesImage at 0x10c299450>

In [13]: m.drawmapscale(50, -75, 0, 0, 400)
Out[13]: 
[<matplotlib.lines.Line2D at 0x11d2e41d0>,
 <matplotlib.lines.Line2D at 0x109cd4d90>,
 <matplotlib.lines.Line2D at 0x11d2e4750>,
 <matplotlib.text.Text at 0x11d2e4d90>,
 <matplotlib.text.Text at 0x11d2e5610>]

enter image description here

Unfortunately the Basemap API doesn't seem to mention anything about it not working for all projections. But here seems to be a potential workaround.

Secede answered 22/12, 2013 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.