Using Lets-Plot with GeoPandas to Create Maps

GeoPandas GeoDataFrame is a tabular data structure that contains a set of shapes (geometry) per each observation.

GeoDataFrame extends pandas DataFrame and as such, aside from the geometry, can contain other data.

GeoPandas has three basic classes of geometric objects (shapes):

  • Points / Multi-Points
  • Lines / Multi-Lines
  • Polygons / Multi-Polygons

All GeoPandas shapes are "undersood" by Lets-Plot and can be plotted using various geometry layers, depending on the type of the shape.

Use:

  • geom_point, geom_text with Points / Multi-Points
  • geom_path with Lines / Multi-Lines
  • geom_polygon, geom_map with Polygons / Multi-Polygons
  • geom_rect when used with Polygon shapes will display corresponding bounding boxes
In [1]:
import geopandas as gpd
from lets_plot import *

LetsPlot.setup_html()
In [2]:
gpd.datasets.available
Out[2]:
['naturalearth_cities', 'naturalearth_lowres', 'nybb']

Polygon shapes - Naturalearth low-resolution world dataset.

In [3]:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.head()
Out[3]:
pop_est continent name iso_a3 gdp_md_est geometry
0 920938 Oceania Fiji FJI 8374.0 MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1 53950935 Africa Tanzania TZA 150600.0 POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2 603253 Africa W. Sahara ESH 906.5 POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3 35623680 North America Canada CAN 1674000.0 MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4 326625791 North America United States of America USA 18560000.0 MULTIPOLYGON (((-122.84000 49.00000, -120.0000...
In [4]:
world.continent.unique()
Out[4]:
array(['Oceania', 'Africa', 'North America', 'Asia', 'South America',
       'Europe', 'Seven seas (open ocean)', 'Antarctica'], dtype=object)
In [5]:
blank_theme = theme_classic() + theme(axis='blank')

geom_polygon()

In [6]:
# Use parameter `map` in `geom_polygon` to display Polygons / Multi-Polygons 
(ggplot() 
 + geom_polygon(map=world, fill='white', color='gray') 
 + ggsize(700, 400) 
 + blank_theme
)
Out[6]:

geom_map()

geom_map() is very similar to geom_polygon() but it automatically applies Mercator projection and other defaults that are more suitable for displaying blank maps.

In [7]:
(ggplot() 
 + geom_map(map=world) 
 + ggsize(700, 400) 
 + blank_theme
)
Out[7]:
In [8]:
# When applying Mercator projection to the world map, Antarctica becomes disproportionally large so 
# in the future lets show only part of it above 85th parallel south:
world_limits = coord_map(ylim=[-70, 85])

Point shapes - Naturalearth world capitals dataset.

In [9]:
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
cities.head()
Out[9]:
name geometry
0 Vatican City POINT (12.45339 41.90328)
1 San Marino POINT (12.44177 43.93610)
2 Vaduz POINT (9.51667 47.13372)
3 Luxembourg POINT (6.13000 49.61166)
4 Palikir POINT (158.14997 6.91664)

geom_point()

In [10]:
# Use parameter `map` in `geom_point` to display Point shapes
(ggplot()
 + geom_map(map=world)
 + geom_point(map=cities, color='red')
 + ggsize(800, 600) + blank_theme + world_limits
) 
Out[10]: