5 Visualizations with Python to Show Simultaneous Changes in Geospatial Data

Author:Murphy  |  View: 20820  |  Time: 2025-03-23 12:13:05
Photo by John Matychuk on Unsplash

Time and space are set as the main themes in some sci-fi movies, such as Interstellar, my favorite. A reason that makes this kind of movie interesting is the stories that happen simultaneously at different locations, and at the same time, they affect each other.

Geospatial data is a type of data that contains spatial information and attributes, which are the characteristics of the event or object.

If considering Data Visualization as a way to tell a story, geospatial data with changes in attributes over time can be a good ingredient for creating an interesting storyline since it shares some concepts with sci-fi movies.

Examples of charts built with Python to express simultaneous changes in geospatial data in this article. Image by author.

This article will guide you with charts and techniques using Python code to express simultaneous changes in Geospatial data.

Let's get started!!


Getting data

Geometry data

Start with importing libraries. We will mainly work with GeoPandas, a useful and easy-to-use library for plotting geospatial data.

Python">import numpy as np
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

Next, the geospatial information can be obtained from geojson.xyz, an open-source website that provides access to GeoJSON data for web mapping examples and experiments.

The geometry data that we are going to use are the boundaries of the states in the western United States. The code below shows how to get the geometry data, excluding Hawaii and Alaska.

This region will be the main area in this article. If you want to select different states, feel free to modify the list below.

Now that we have the geometries, let's plot the geospatial data.

A base map can be added as a background using the contextily library to make the chart look realistic. The method is shown in the code below.

Plotting geospatial data using GeoPandas. Image by author.

Data values

To show that the methods in this article can be applied to real-world data, ‘U.S. states and territories by income' information on Wikipedia will be used as an example for expressing annual changes in data values.

The data are used under the CC BY-SA 4.0 International license.

The information can be downloaded with just a few lines of code using the Panda's read_html function. Please note that the retrieved table has no information from the year 2020.

Next, the obtained DataFrame will be merged with the previous one.

df_m = pd.merge(df, df_w, on='name')
df_m.head()

The median household income values must be converted into integers since the data values obtained are in string format. By the way, the values in string format are kept for annotating the output charts.


Visualization

This article will guide with 5 chart ideas to show simultaneous changes in geospatial data.

Let's get started!!

1. Using spatial areas and colors with Choropleth maps

A Choropleth map is a typical spatial chart that displays colors in accordance with geographic characteristics. By combining multiple maps, we can express the changes over time.

Starting with the code below, the loop function will be applied to plot the choropleth map yearly. Besides color mapping, annotating data values can help show the progress of the median income values.

Please note that the charts obtained will be saved onto your computer for importing in the next step.

An example of a choropleth map. Image by author.

Now that we already have multiple charts, the following methods are ideas for combining them.

1.1 Creating a photo collage

The first idea is to combine the charts all together as a photo collage. This can be considered a good choice for creating an infographic.

Let's define a function for doing the task. I found this useful code and method to create a photo collage from this link on Stack Overflow.

Next, the charts will be imported and combined with the defined function.

Voila!!

A part of a photo collage combining choropleth maps. Image by author.

The photo collage can show and help compare the simultaneous changes in geospatial data. From the result, it can be easily distinguished that California has the highest median household income, and New Mexico has the lowest one over time.

1.2 Creating an animation

Besides putting the maps next to each other, we can gather them in order to create an animation. If the media you use allows displaying moving pictures in a GIF file format, this can be a good choice for catching the reader's attention.

Ta-da!!

An animation of choropleth maps showing simultaneous changes in geospatial data. Image by author.

2. Comparing sizes with Bubble charts

With the same concept as the previous method, circle areas in bubble charts can be applied to represent data values. The bubbles are located at the state centroids, while the size will change following annual data values.

For mapping colors later, we are going to extract a color palette to create a list of colors.

Next, plot the bubble chart each year using Python's for-loop function.

An example of a bubble chart on a spatial plot. Image by author.

After creating the bubble charts, we will combine them to express changes in spatial data.

2.1 Creating a photo collage

Use the previously defined function to get a photo collage.

get_collage(3, 4, 900*3, 650*4,
            ['bubble_'+y+'.png' for y in y_list], 'bubble_collage.png') 
A part of a photo collage combining bubble charts. Image by author.

2.2 Creating an animation

Put the charts together to create a moving GIF file.

img = [Image.open('bubble_'+y+'.png') for y in y_list]
imageio.mimsave('bubble.gif', img, duration=0.4)
An animation of bubble charts showing simultaneous changes in geospatial data. Image by author.

Compared with the choropleth maps, besides labeling data and mapping with colors, the circle areas also help compare spatial data values. We can see that the circle sizes are updated over time.


3. Showing proportions with Pie charts

If the goal is to compare data values locally, pie charts can be applied to facilitate reading since they are able to show each location's proportion compared with the others.

To make each location's data stand out, the trick is to separate each location's piece from the rest. This will help distinguish the proportion more easily. This can be set at the ‘explode' parameter, as shown in the code below.

An example of a pie chart on a spatial plot. Image by author.

Creating a photo collage

Let's combine the results.

get_collage(3, 4, 980*3, 750*4,
            ['pie_'+ y +'.png' for y in y_list],
            'pie_collage.png')
A part of a photo collage combining pie charts. Image by author.

Creating an animation can also be conducted using the previously mentioned method, as shown with the choropleth maps and bubble charts.


4. Making things simple with Bar charts

So far, the techniques previously mentioned are all combining multiple charts. They need to be put together as a photo collage or an animation. This can be a limit when a simple single chart is preferred.

No problem!!

A simple single chart will do as well. Everything can be plotted in just one graph by applying the bar charts. The main concept is each location has its own bar chart in which each bar represents the value of each year. The color mapping will be applied to help compare the bar values.

This method can be an alternative option to choropleth maps and bubble charts in this article.

Ta-da!!

Plotting multiple bar charts on a spatial plot. Image by author.

5. Handling time-series data with Line charts

In fact, the annual data that we have is time-series data. We can apply multiple line charts to express changes over time. Silhouettes of other locations' data can be shown locally to facilitate reading and help compare the values.

This method can be an alternative option to pie charts in this article since it helps focus on each location's data while being able to compare with the rest.

Plotting multiple line charts on a spatial plot to express time-series data. Image by author.

Summary

Firstly, let's wrap up every chart mentioned in this article.

  1. Choropleth maps
  2. Bubble charts
  3. Pie charts
  4. Bar charts
  5. Line charts

If we take a close look at these charts, most are just common graphs with nothing sophisticated. The main idea is to apply their simplicity to show complicated scenarios: simultaneous data value changes in multiple locations over time.

"Out of clutter, find simplicity" – Albert Einstein

Lastly, I'm pretty sure there can be other graphs for this kind of data. The main purpose of this article is to guide with ideas, and the charts explained here are just examples.

If you want to share or have any suggestions, please feel free to leave a comment.

Thanks for reading.


Here are some of my data visualization articles that you may find interesting:

  • 7 Visualizations with Python to Handle Multivariate Categorical Data (link)
  • 8 Visualizations with Python to Handle Multiple Time-Series Data (link)
  • 7 Visualizations with Python to Express changes in Rank over time (link)
  • 9 Visualizations with Python to show Proportions or Percentages instead of a Pie chart (link)
  • 9 Visualizations with Python that Catch More Attention than a Bar Chart (link)

References

Tags: Data Science Data Visualization Geospatial Python Tips And Tricks

Comment