python4oceanographers

Turning ripples into waves

Creating a correct map of Brazilian states with basemap

Creating a map of Brazil with correct states boundaries with basemap.

basemap is an excellent tool, but its database lacks up-to-date information regarding the modern states boundaries in Brazil. This post is to show how to overcome that with an custom shapefile.

The political boundaries data shipped with basemap is OK for North America and Europe, but it is outdated by several years for South America. In order to draw a more accurate map we need to use custom data from more up-to-date sources.

One source of data are the shapefiles from the "GADM database of Global Administrative Areas". With that data and matplotlib's Polygon we can crate a similar drawstates function to override basemap's defaults.

In [2]:
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap


def make_map():
    fig, ax = plt.subplots()
    m = Basemap(projection='merc', llcrnrlat=-35, urcrnrlat=7,
                llcrnrlon=-77, urcrnrlon=-32, resolution='i')
    m.ax = ax
    m.fillcontinents()
    return fig, m

Now let's define a new drawstates that makes use of the updated dataset we downloaded:

In [3]:
def drawstates(ax, shapefile='/home/filipe/00-NOBKP/OcFisData/brazil/brazil'):
        shp = m.readshapefile(shapefile, 'states', drawbounds=True)
        for nshape, seg in enumerate(m.states):
            poly = Polygon(seg, facecolor='0.75', edgecolor='k')
            ax.add_patch(poly)

Finally, let's plot the original basemap method to draw states:

In [4]:
fig, m = make_map()
m.drawcountries()
_ = m.drawstates()

And our custom shapefile:

In [5]:
fig, m = make_map()
drawstates(m.ax)

See the difference?

In [6]:
HTML(html)
Out[6]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Comments