Creating Interactive Maps with Altair: A Step-by-Step Guide
Introduction: Data visualization is crucial in effectively communicating insights and patterns hidden within data. In this article, we will explore how to create interactive maps using Altair, a powerful Python library for declarative visualization. Following this step-by-step guide will teach you how to transform your geospatial data into captivating and informative maps.
Step 1: Import the Required Libraries
To get started, import Altair, Pandas, and any additional libraries needed for data manipulation and visualization. Here’s an example of the necessary import statements:
import altair as alt
import pandas as pd
from vega_datasets import data
import matplotlib.pyplot as plt
Step 2: Loading and Preparing the Data
Load your mass shooting data into a Pandas DataFrame. Ensure that the data includes relevant geospatial information, such as latitude and longitude coordinates. I got the data from this website. I also create bins for the TOTALVICTIMS
column so the legends are concise and easy to read:
Step 3: Creating the map using Altair
To create a map in Altair, we need a base map that provides the geographical context. Use a TopoJSON feature to define the base map.
states = alt.topo_feature(data.us_10m.url, feature='states')
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).project('albersUsa').properties(
width=600,
height=400
)
Plot the mass shooting incidents as data points on the map. Use the `mark_circle()` function to represent each incident as a circle, customizing the size and color encoding based on the number of casualties.
points = alt.Chart(shooting_df).mark_circle().encode(
longitude='LONGITUDE',
latitude='LATITUDE',
size=alt.value(50),
color=alt.Color('Bin:N',
sort = ['1 to 5','6 to 10','10 to 20','20 to 30','>30' ],
scale=alt.Scale( range=['red','violet','gold','green']),
legend=alt.Legend(title="Total Victims")
)
Combine the base map and the data points using the “+” operator to create the final map visualization. This overlays the data points on the base map, providing a complete geographical representation. Here’s an example:
).properties(
title= ['Mass shootings occurring in the US from 1982-2019', 'Mass shootings: 4+ victims killed, excluding robbery or gang violence.']
)
chart = background + points
chart
chart.configure_legend(
gradientLength=250,
gradientThickness=30,
gradientDirection ='vertical',
gradientHorizontalMinLength = -2)
Here is the full code put together:
states = alt.topo_feature(data.us_10m.url, feature='states')
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).project('albersUsa').properties(
width=600,
height=400
)
points = alt.Chart(shooting_df).mark_circle().encode(
longitude='LONGITUDE',
latitude='LATITUDE',
size=alt.value(50),
color=alt.Color('Bin:N',
sort = ['1 to 5','6 to 10','10 to 20','20 to 30','>30' ],
scale=alt.Scale( range=['red','violet','gold','green']),
legend=alt.Legend(title="Total Victims")
)
).properties(
title= ['Mass shootings occurring in the US from 1982-2019', 'Mass shootings: 4+ victims killed, excluding robbery or gang violence.']
)
chart = background + points
chart
chart.configure_legend(
gradientLength=250,
gradientThickness=30,
gradientDirection ='vertical',
gradientHorizontalMinLength = -2)
The Map:
Conclusion:
In this article, we explored the process of creating interactive maps using Altair. By following the steps outlined, you can effectively visualize mass shooting incidents in the US, shedding light on their geographical impact. Connect with me on LinkedIn.