How to Create Map Plots with Plotly

Author:Murphy  |  View: 23355  |  Time: 2025-03-23 12:45:01
Photo by Willian Justen de Vasconcellos on Unsplash

Plotly is a great open source library for visualizing data. In this blog post, I am going to show you how to generate cartographic plots with plotly, working with the Python backend.

For illustration purposes, I will use the Significant Volcanic Eruption Database, published by the US National Centers for Environmental Information under the U.S. Government Work License. The dataset is available for download here: https://public.opendatasoft.com/explore/dataset/significant-volcanic-eruption-database/information/

You are going to see the following five visualizations:

  1. Global distribution of significant volcanic eruptions
  2. Volcano types in North America
  3. Volcanic eruptions associated with tsunamis
  4. Most damaging volcanic eruptions
  5. Funny map projections

For readers interested in using plotly for Data Analysis, please refer to my recent post on visualizing data from the Women's World Cup:

FIFA Women's World Cup 2023 visualized with Plotly


Preparing the data

After downloading the volcanic eruption database, we load it as a pandas DataFrame. DataFrames integrate naturally with Plotly and are convenient for data analysis. We transform the columns that encode whether a volcanic eruption is associated with a volcano or an earthquake to True/False values and add new columns for the latitude and longitude of an eruption.

Global distribution of significant volcanic eruptions

The first visualization shows the global distribution of significant volcanic eruptions. This is a scatter_geo scatter plot, using longitude and latitude as x and y coordinates, respectively. The map projection is set to natural earth , and the individual events are colored and increased in size according the their Volcanic Explosivity Index. For the color scale, I chose the inbuilt magma sequential scale that seemed fitting for the volcano topic.

Volcanic explosivity index scatter plot. Image: Author.

As we can see, volcanic eruptions do not occur evenly distributed across the world, but are centered in certain regions. These are related to tectonic boundaries, e.g. at the American west coast and the Pacific fire belt.

This is the code to generate the visualization:


Volcano types in North America

There are different types of volcanoes, and some are more frequent than others. In the next visualization, we focus on North America. Again, we use a scatter_geo plot, and use the keyword argument scope='north america' to automatically set the displayed map extent to the desired region.

We observe the famous Hawaiian shield volcanoes, and the large number of stratovolcanoes along the tectonic boundaries at the Pacific coast.

Volcano types in North America. Image: Author.

The visualization is generated with the following code:


Volcanic eruptions associated with tsunamis

Volcanic eruptions are often associated with other catastrophic events like earthquakes and tsunamis. In this visualization, we focus on the Pacific ocean and display volcanic events on a terrain map. The color encodes whether an event was accompanied by a tsunami.

The mapbox tiles are available via scatter_mapbox .

Significant volcanic eruptions accompanied by Tsunamis. Image: Author.

This visualization is generated with the following code:


Most deadly volcanic eruptions

We now turn to the side effects of volcanic eruptions. Eruptions of the size that are covered in the dataset are huge natural disasters, and are often accompanied by a significant loss of human lives.

In this visualization, we use a density plot overlayed with mapbox tiles to illustrate the deadly impact of volcanic eruptions. The mapbox tiles are cartographic, displaying state boundaries rather than a terrain map as in the previous visualization. The color scale intensity signifies the total number of deaths associated with an individual eruption.

Deadly impact of volcanic eruptions. Image: Author.

Eruptions that take place in populated areas are likely to cause more damage. From the map, the island archipelago of Indonesia sticks out. This region is part of the volcanically active pacific fire belt, and at the same time is densely populated. In Europe, the catastrophic eruption of Vesuvius in Roman times led to a high death toll.

The code to generate this visualization is as follows:


Funny map projections

Since Earth is not flat, any representation of its surface in a two-dimensional plane has to resort to a map projection. Projections can emphasize different aspects of the map [https://en.wikipedia.org/wiki/Map_projection].

Plotly offers a wide range of in-built map projections that can be used together with the geoplots. To choose another map projection, change the projection keyword in scatter_geo and other plotting functions.

Here, we visualize again volcanoes colored by their explosivity, but on a Foucaut map projection. This is a type of area-preserving projection, i.e areas are not inflated in size the farther away they are from the equator. Note how tiny Greenland appears, compared to what we are used to from standard map projections!

Foucaut projection. Image: Author.

Summary

We generated five visualizations using cartographic scatter plots from the plotly library. Plotly offers terrain maps that can be automatically added to the plot using tiles from the mapbox library. The plots are customizable in a similar way as standard plots.

Cartographic plots are great to illustrate the regional impact of events in a dataset. Compared to other packages, such as cartopy and folium, the plotly library offers less functionality. However, if you are interested in quickly visualizing your data on a map, plotly enables you to do that without getting into details of geospatial data formats.

Further reading and references

Check out the notebook to generate the plots on Github:

medium_notebooks/plotly/volcanic_activity.ipynb at main · crlna16/medium_notebooks

Tags: Data Analysis Data Visualization Plotly Programming Science

Comment