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.

Loading some example data:

In [1]: world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

In [2]: cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))

We can now plot those GeoDataFrames:

# Examine country GeoDataFrame
In [3]: world.head()
Out[3]: 
     pop_est  ...                                           geometry
0     920938  ...  (POLYGON ((180 -16.06713266364245, 180 -16.555...
1   53950935  ...  POLYGON ((33.90371119710453 -0.950000000000000...
2     603253  ...  POLYGON ((-8.665589565454809 27.65642588959236...
3   35623680  ...  (POLYGON ((-122.84 49.00000000000011, -122.974...
4  326625791  ...  (POLYGON ((-122.84 49.00000000000011, -120 49....

[5 rows x 6 columns]

# Basic plot, random colors
In [4]: 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.

Choropleth Maps

geopandas makes it easy to create Choropleth 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 [5]: world = world[(world.pop_est>0) & (world.name!="Antarctica")]

In [6]: world['gdp_per_cap'] = world.gdp_md_est / world.pop_est

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

Creating a legend

When plotting a map, one can enable a legend using the legend argument:

# Plot population estimates with an accurate legend
In [8]: import matplotlib.pyplot as plt

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

In [10]: world.plot(column='pop_est', ax=ax, legend=True)
Out[10]: <matplotlib.axes._subplots.AxesSubplot at 0x7f818d8ca588>
_images/world_pop_est.png

However, the default appearance of the legend and plot axes may not be desirable. One can define the plot axes (with ax) and the legend axes (with cax) and then pass those in to the plot call. The following example uses mpl_toolkits to vertically align the plot axes and the legend axes:

# Plot population estimates with an accurate legend
In [11]: from mpl_toolkits.axes_grid1 import make_axes_locatable

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

In [13]: divider = make_axes_locatable(ax)

In [14]: cax = divider.append_axes("right", size="5%", pad=0.1)

In [15]: world.plot(column='pop_est', ax=ax, legend=True, cax=cax)
Out[15]: <matplotlib.axes._subplots.AxesSubplot at 0x7f818d9b9f60>
_images/world_pop_est_fixed_legend_height.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 [16]: 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 mapclassify installed, which can be accomplished via conda install -c conda-forge mapclassify). The scheme option can be set to any scheme provided by mapclassify (e.g. ‘box_plot’, ‘equal_interval’, ‘fisher_jenks’, ‘fisher_jenks_sampled’, ‘headtail_breaks’, ‘jenks_caspall’, ‘jenks_caspall_forced’, ‘jenks_caspall_sampled’, ‘max_p_classifier’, ‘maximum_breaks’, ‘natural_breaks’, ‘quantiles’, ‘percentiles’, ‘std_mean’ or ‘user_defined’). Arguments can be passed in classification_kwds dict. See the mapclassify documentation for further details about these map classification schemes.

In [17]: 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 little 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 [18]: cities.plot(marker='*', color='green', markersize=5);

# Check crs
In [19]: cities = cities.to_crs(world.crs)

# 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 [20]: base = world.plot(color='white', edgecolor='black')

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

Method 2: Using matplotlib objects

In [22]: import matplotlib.pyplot as plt

In [23]: 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 [24]: ax.set_aspect('equal')

In [25]: world.plot(ax=ax, color='white', edgecolor='black')
Out[25]: <matplotlib.axes._subplots.AxesSubplot at 0x7f818cc75898>

In [26]: cities.plot(ax=ax, marker='o', color='red', markersize=5)
Out[26]: <matplotlib.axes._subplots.AxesSubplot at 0x7f818cc75898>

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

Other Resources

Links to jupyter Notebooks for different mapping tasks:

Making Heat Maps