Map-based Visualization libraries for Python: Comparison and Tutorials

Yash Sanghvi
Tech@Carnot
Published in
6 min readAug 10, 2020

--

Map-based visualizations are an essential aspect of any data-presentation/ inference. In fact, it is often stated that “80% of all information is geospatially referenced”. How accurate the 80% figure is, is up for debate, but it does drive home the point that learning map-based visualization is inevitable if you are a data scientist. Geo-spatial visualizations become all the more important for IoT companies like Carnot which have their devices scattered across different geographies. We at Carnot realized the need for having a robust map-based visualization infrastructure for all our present and future requirements. When we started building this infrastructure, we realized that there are way too many libraries available for map visualizations and several detailed tutorials for each library. However, what wasn’t readily available was a comparison of the different libraries and a guide on which library is best suited for a particular application.

Hence, we started with one library at a time, determined what it was best suited for, and then moved on to the next. We kept doing this exercise until we generated nearly all the types of visualizations we could envision. This post is a summary of our learning during this entire exercise and it will hopefully serve as a guide to people who find themselves as overwhelmed with choices as we were at the start of our infra-building exercise.

Scope:

Considering the number of map-based visualization libraries available for python, it is nearly impossible (and not helpful either) to cover every library. As mentioned above, we kept exploring libraries until we were able to find a good one for all the visualizations that we had envisioned. This post, therefore, will be limited to the comparison of those libraries only. They are:

  1. Cartopy
  2. Geopandas
  3. Plotly
  4. Plotly with Mapbox
  5. Plotly with Datashader
  6. Folium

The tutorials will also be provided for the above libraries only.

Visualizations that will be covered using these libraries:

We will primarily focus on scatter and choropleth visualizations. We want to create static images, time-lapse videos, interactive visualizations, for big data as well as nominal data. The featured image gives a good idea of the targeted visualizations.

Summary of the comparison of the libraries:

A comparison of the different map-based visualization Python libraries
Interactivity vs Resource Usage (during computation) for different libraries

Detailed description and links to the tutorial of each library:

The following GitHub repo contains all the notebooks and dummy data that’ll be used for the tutorials: https://github.com/carnot-technologies/MapVisualizations. It is recommended to clone this repo on your local machine.

Cartopy:

Changing agri-hotspots from data gathered from 1000s of farm machines across several parts of India. See Simha Kit

This is your choice of library if you want to use a custom background image for your scatter visualization. The background in the above video belongs to NASA. Please note that only world-map background images are allowed, so if you have a custom image for a specific region, it won’t work. This library helps you produce a static non-interactive image. Multiple images can be stitched together to create a time-lapse video as illustrated above. For a detailed tutorial on this library, click here.

GeoPandas:

This is your choice of library for creating static choropleth visualizations. This library required a shapefile for the region of interest to produce the visualization. You need not have the shapefile for the entire world/ country. If a district is your region of interest, a shapefile for that district will suffice. Like Cartopy, this also produces non-interactive images that can be stitched together to form a time-lapse video. For a detailed tutorial on this library, and for information on where to get the shapefiles from, click here.

Plotly:

This library is at the pinnacle of interactivity. Hover over data to get more information. Zoom into the map, pan the map, rotate the map. Create live animations, fully loaded with buttons and sliders. There’s hardly anything that this library can’t do. It can be used for creating both scatter and choropleth visualizations (it requires a GeoJSON file instead of shapefile for choropleth maps). You can download the animations as an HTML file. You can embed the animations in your website/ blog. We believe that being acquainted with this library is essential if you are learning map-based visualizations. Find a detailed tutorial for this library here.

Plotly with Mapbox:

The above video shows how the UP story, visualized earlier using GeoPandas, becomes much more informative and enhanced using plotly and Mapbox.

Mapbox provides maps in the tile format. There are several useful free map templates one can choose from. When used for making a choropleth map, the GeoJSON shapes overlay on the Mapbox map. All the interactivity of plotly is preserved. For a detailed scatter tutorial, click here. For a detailed choropleth tutorial, click here.

Plotly with Datashader:

Datashader is a library built specifically for handling large datasets (millions of lat-lon coordinates). The above image, for example, contains 3 million locations. Datashader converts lat-lon data into a pixel-density raster. Now, the intensity of each pixel is determined by the number of locations lying in that pixel. This helps produce really cool visualizations like the one shown above, using color palettes like fire. It is partially interactive, i.e., you can zoom into the map and pan it. But clicking/hovering on any pixel won’t have any effect, because datashader essentially overlays a static image on the map. For a detailed tutorial, click here.

Folium:

Folium helps create both scatter and choropleth maps. You can use this library if you are working with TopoJSON files, which are much more compact than GeoJSON files. However, folium can’t do a lot of things that other libraries like Plotly can. For instance, hovering on any state in the above map will not display any information, unlike Plotly.

The detailed tutorial for Folium, using aTopoJSON file, can be found here.

Closing thoughts:

Several libraries and visualizations have been covered through the tutorials linking to this post. A comparison of the libraries has been provided to help decide which library to use for a particular application.

However, this is certainly not the end of the story. There are several other python libraries for data visualization that you can try out. Bokeh, HoloViews are popular examples of libraries not covered.

Apart from python libraries, there are several other online tools that help create beautiful map-based visualizations. kepler.gl, for example, is a wonderful tool for creating 3D visualizations, as shown in the image below.

So use this post as a good starting point in your map-based visualization journey. Keep learning (we are learning too!).

We are trying to fix some broken benches in the Indian agriculture ecosystem through technology, to improve farmers’ income. If you share the same passion join us in the pursuit, or simply drop us a line on report@carnot.co.in

Follow Tech@Carnot for more such blogs on topics like Data Science and Visualization, Cloud Engineering, Firmware Development, and many more.

--

--