Mapping Tools

geopandas provides a high-level interface to the matplotlib library for making maps. Mapping shapes is as easy as using the plot() method on a GeoSeries or GeoDataFrame.

# Examine country GeoDataFrame
In [1]: world.head()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-6e4c6b8ad2c1> in <module>()
----> 1 world.head()

NameError: name 'world' is not defined

# Basic plot, random colors
In [2]: world.plot();
_images/world_randomcolors.png

Note that in general, any options one can pass to pyplot in matplotlib (or style options that work for lines) can be passed to the plot() method.

Chloropleth Maps

geopandas makes it easy to create Chloropleth maps (maps where the color of each shape is based on the value of an associated variable). Simply use the plot command with the column argument set to the column whose values you want used to assign colors.

# Plot by GDP per capta
In [3]: world = world[(world.pop_est>0) & (world.name!="Antarctica")]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-3d65aa1f3698> in <module>()
----> 1 world = world[(world.pop_est>0) & (world.name!="Antarctica")]

NameError: name 'world' is not defined

In [4]: world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-b9fbf9f3897f> in <module>()
----> 1 world['gdp_per_cap'] = world.gdp_md_est / world.pop_est

NameError: name 'world' is not defined

In [5]: world.plot(column='gdp_per_cap');
_images/world_gdp_per_cap.png

Choosing colors

One can also modify the colors used by plot with the cmap option (for a full list of colormaps, see the matplotlib website):

In [6]: world.plot(column='gdp_per_cap', cmap='OrRd');
_images/world_gdp_per_cap_red.png

The way color maps are scaled can also be manipulated with the scheme option (if you have pysal installed, which can be accomplished via conda install pysal). By default, scheme is set to ‘equal_intervals’, but it can also be adjusted to any other pysal option, like ‘quantiles’, ‘percentiles’, etc.

In [7]: world.plot(column='gdp_per_cap', cmap='OrRd', scheme='quantiles');
_images/world_gdp_per_cap_quantiles.png

Maps with Layers

There are two strategies for making a map with multiple layers – one more succinct, and one that is a littel more flexible.

Before combining maps, however, remember to always ensure they share a common CRS (so they will align).

# Look at capitals
# Note use of standard `pyplot` line style options
In [8]: cities.plot(marker='*', color='green', markersize=5);

# Check crs
In [9]: cities = cities.to_crs(world.crs)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-31469edf5e1f> in <module>()
----> 1 cities = cities.to_crs(world.crs)

NameError: name 'cities' is not defined

# Now we can overlay over country outlines
# And yes, there are lots of island capitals
# apparently in the middle of the ocean!
_images/capitals.png

Method 1

In [10]: base = world.plot(color='white')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-91e7c0d2b84e> in <module>()
----> 1 base = world.plot(color='white')

NameError: name 'world' is not defined

In [11]: cities.plot(ax=base, marker='o', color='red', markersize=5);
_images/capitals_over_countries_1.png

Method 2: Using matplotlib objects

In [12]: import matplotlib.pyplot as plt

In [13]: fig, ax = plt.subplots()

# set aspect to equal. This is done automatically
# when using *geopandas* plot on it's own, but not when
# working with pyplot directly.
In [14]: ax.set_aspect('equal')

In [15]: world.plot(ax=ax, color='white')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-9fc582f480d1> in <module>()
----> 1 world.plot(ax=ax, color='white')

NameError: name 'world' is not defined

In [16]: cities.plot(ax=ax, marker='o', color='red', markersize=5)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-16-ec592ff81f19> in <module>()
----> 1 cities.plot(ax=ax, marker='o', color='red', markersize=5)

NameError: name 'cities' is not defined

In [17]: plt.show();
_images/capitals_over_countries_2.png

Other Resources

Links to jupyter Notebooks for different mapping tasks:

Making Heat Maps