Introducing gspatial-plot, a simplified plotting interface for geopandas

Pareekshith Katti
3 min readFeb 27, 2023

Geopandas is one of the most essential library for geospatial processing in python and is a core part of geospatial data science stack. For plotting basic plots, geopandas is simple and great, but if you want to plot different types of thematic maps such as a bubble plot or a cartogram, or if you want to plot a density map, you’ll find yourself doing a lot of manipulation and tinkering around matplotlib to produce a good looking graph.

We wanted a solution where we can produce a good looking map with a single line of code and still be able to customize the plot the way we want. That’s why we developed gspatial-plot, a simplified plotting interface for geopandas. With gspatial plot, these were our goals:

  1. A very simple API
  2. Better default settings for plots
  3. Easy way to customize
  4. Should be compatible with other geopandas and matplotlib axes
  5. Simple functions which take in just the bare minimum information needed for plotting thematic maps

You can install gspatial_plot using:

pip install gspatial-plot

Example: You can generate a cartogram in a single line of code

gsp.cartogram(
usa,
"AWATER",
legend_kwds={"loc": "lower left"},
cmap = "Blues"
)

And tweak different aspects of the graph

gsp.cartogram(
usa,
usa["AWATER"],
cmap="Blues",
figsize=(30, 30),
cartogram_only=True,
legend_kwds={
"loc": "lower left",
"bbox_to_anchor": (0, 0.2),
"prop": {"family": "Sawasdee", "weight": 3, "size": 22},
},
title="USA TOTAL WATER AREA",
title_kwds={
"fontsize": 50,
"fontname": "Sawasdee",
"fontweight": 3,
"loc": "right",
},
)

Our main aim was to make seaborn-like interface for gspatial plot. There already exists a library called geoplot that aims to do this but by no means, gspatial-plot is a drop in replacement and there are some methods available in geoplot that aren’t implemented in gspatial-plot and vice versa.

gspatial-plot provides a simple way to plot choropleth maps, bubble maps, cartograms, density plots, heat maps, and spike maps and it also implements replacements to default geopandas plots such as pointplot and shapeplot. These do the same thing as geopandas plot function but have better defaults and more easy to access settings. We also have a function called randommap which assigns each shape in a geodataframe a color based on a predefined list of colors or a custom list of colors. We also can annotate the shapes easily by passing annot=True and the annotation column such as the name of the geographic feature.

A default shapeplot

Apart from this, gspatial-plot also has two more useful functions — offline_static_basemap and offline_folium_basemap. These two functions use natural earth vector data in order to provide a basemap. The color, opacity, and edge thickness of land, ocean, boundaries, and lines are customizable and it does not need an active internet connection. This is useful in situations where you need a basic basemap to show data but you do not need nuances that an online basemap service like OpenStreetMap provides.

An offline static basemap with points on it

gspatial-plot is in beta, it is tested with python 3.10 and 3.9 and it is fairly stable from our testing, if you find any issues, please report them on GitHub!

You can find the links to the project below:

Github (Please star it, it helps a lot!) — GitHub — ambeelabs/gspatial_plot: Simplifying geospatial plots in python. Aims to be seaborn equivalent for geospatial plots. Built on top of geopandas.

PyPi — gspatial-plot · PyPI

Docs — Welcome to gspatial-plot’s documentation! — gspatial-plot 0.1.0a0 documentation

gspatial-plot is licensed under a permissive MIT License.

More posts that include tutorials and recipes will be released on my medium so keep an eye, you can also follow me and that’d be great.

--

--