Skip to content

Latest commit

 

History

History

geopandas

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Geographic Maps

Let's say you want to plot a map of Europe, showing major cities and shading countries by popluation.

If you haven't already, you first need to install some things to make maps:

pip3 install --upgrade pip
pip3 install geopandas shapely descartes geopy
sudo apt install -y python3-rtree

Paste+run the following:

import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))

eu = world[world["continent"] == "Europe"]
ax = eu.plot(column="pop_est")
geopandas.sjoin(cities, eu).plot(ax=ax)

It should look like this:

Can't see much there! We'll walk you through a series of changes to make it look like this much-better version:

Re-generate the map after each step so you can see the result of your changes.

Step 1: Latitude/Longitude Limits

At the end of the cell, paste these calls:

ax.set_xlim(-25, 45)
ax.set_ylim(30, 80)

Step 2: Country Styling

Let's use the "Oranges" color map, put a light gray border around countries, and increase the color size. In the above snippet, change the ax = eu.plot(...) line to this:

ax = eu.plot(column="pop_est", cmap="Oranges", edgecolor="0.8", figsize=(8,8))

Step 3: City Styling

Let's use small black markers for cities by passing color and markersize to the second .plot call:

geopandas.sjoin(cities, eu).plot(ax=ax, color="black", markersize=3)

Step 4: Removing Axes

Although there are exceptions, axes are often less useful for maps than other plots, so add this to turn them off:

ax.set_axis_off()

Summary

If you got stuck above, here are all the changes together:

import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))

eu = world[world["continent"] == "Europe"]
ax = eu.plot(column="pop_est", cmap="Oranges", edgecolor="0.8", figsize=(8,8))
geopandas.sjoin(cities, eu).plot(ax=ax, color="black", markersize=3)
ax.set_xlim(-25, 45)
ax.set_ylim(30, 80)
ax.set_axis_off()