How to Create Comprehensive Area Charts with Altair

How to Create Comprehensive Area Charts with Altair

Altair provides a variety of options when creating area charts, which are particularly effective for visualizing time-series data and showing how quantities change over time. In this article, we’ll explore different types of area charts using unemployment data across various industries from 2000 to 2010 from the vega_datasets package.  The visuals will focus on three industries that were central to the 2008-2009 financial crisis: Construction (housing market collapse), Manufacturing (industrial impact), and Finance (banking system shock). We’ll progress from basic to more complex visualizations, demonstrating Altair’s flexibility and expressive functions. If you need any assistance setting up your Altair environment or the datasets package, please take a look at this post or the official documentation. 

Basic Area Chart

Let’s start with a simple area chart showing unemployment in the manufacturing sector:

 
# 1. Basic Area Chart (Manufacturing only)

import altair as alt
from vega_datasets import data

# Load the unemployment dataset
unemployment = data.unemployment_across_industries()
manufacturing = unemployment[unemployment.series == 'Manufacturing']

basic_area = alt.Chart(manufacturing).mark_area(
    color='#2196F3',
    opacity=0.7,
    line=True
).encode(
    x=alt.X('date:T', 
            title='Date',
            axis=alt.Axis(format='%Y')),
    y=alt.Y('count:Q',
            title='Number of Unemployed (Thousands)',
            scale=alt.Scale(domain=[0, manufacturing['count'].max() * 1.1])),
    tooltip=[
        alt.Tooltip('date:T', title='Date', format='%b %Y'),
        alt.Tooltip('count:Q', title='Unemployed (Thousands)', format=','),
        alt.Tooltip('rate:Q', title='Industry Unemployment Rate', format='.1f')
    ]
).properties(
    width=700,
    height=300,
    title={
        "text": "Manufacturing Sector Unemployment (2000-2010)",
        "subtitle": "Monthly unemployment numbers and sector-specific rates"
    }
)

basic_area

This code introduces several key Altair concepts:

  • mark_area(): Creates an area chart with optional parameters for customization
  • opacity: Controls the transparency of the filled area
  • line=True: Adds a line at the top edge of the area
  • Custom tooltip formatting with multiple fields
  • Scale adjustment to add padding above the maximum value
  • Date formatting using the %Y format string

The resulting visualization shows the manufacturing sector’s unemployment trend, with a dramatic increase during the 2008-2009 financial crisis.

Area graph showing manufacturing sector unemployment from 2000-2010, with a sharp increase starting in 2008.
Area graph showing manufacturing sector unemployment from 2000-2010, with a sharp increase starting in 2008 | Image by Author

Faceted Area Charts

Next, we’ll create multiple area charts for different industries:

 
# 2. Faceted Area Chart

industries_of_interest = ['Manufacturing', 'Construction', 'Finance']
selected_data = unemployment[unemployment.series.isin(industries_of_interest)]

faceted_area = alt.Chart(selected_data).mark_area(
    opacity=0.7,
    line=True
).encode(
    x=alt.X('date:T',
            title='Date',
            axis=alt.Axis(format='%Y')),
    y=alt.Y('count:Q',
            title='Number of Unemployed (Thousands)'),
    color=alt.Color('series:N', 
                    title='Industry',
                    scale=alt.Scale(scheme='set2')),
    tooltip=[
        alt.Tooltip('date:T', title='Date', format='%b %Y'),
        alt.Tooltip('count:Q', title='Unemployed (Thousands)', format=','),
        alt.Tooltip('rate:Q', title='Industry Unemployment Rate', format='.1f')
    ],
    facet=alt.Facet('series:N', 
                    title=None,
                    columns=1)
).properties(
    width=700,
    height=200
)

faceted_area

This visualization introduces:

  • facet: Creates separate charts for each industry
  • columns=1: Arranges charts vertically
  • Color encoding using the ‘set2’ color scheme
  • Consistent scales across all facets
  • Individual height setting for each facet

The faceted view allows easy comparison of unemployment patterns across industries while maintaining separate y-axes for clarity.

Three separate area graphs displaying unemployment trends from 2000-2010 for construction (top), finance (middle), and manufacturing (bottom) sectors.
Three separate area graphs displaying unemployment trends from 2000-2010 for construction (top), finance (middle), and manufacturing (bottom) sectors | Image by Author

Overlapping Area Charts

For direct comparison, we can overlay the area charts:

 
# 3. Overlapping Area Chart

layered_area = alt.Chart(selected_data).mark_area(
    opacity=0.3
).encode(
    x=alt.X('date:T',
            title='Date',
            axis=alt.Axis(format='%Y')),
    y=alt.Y('count:Q',
            title='Number of Unemployed (Thousands)',
            stack=None),
    color=alt.Color('series:N', 
                    title='Industry',
                    scale=alt.Scale(scheme='set2')),
    tooltip=[
        alt.Tooltip('date:T', title='Date', format='%b %Y'),
        alt.Tooltip('series:N', title='Industry'),
        alt.Tooltip('count:Q', title='Unemployed (Thousands)', format=','),
        alt.Tooltip('rate:Q', title='Industry Unemployment Rate', format='.1f')
    ]
)

layered_area

Key features demonstrated:

  • stack=None: Prevents stacking of areas
  • Lower opacity (0.3) for better visibility of overlapping areas
  • Industry-specific coloring
  • Enhanced tooltip with industry identification

The overlapping view reveals how unemployment trends correlate across industries during key economic events.

Area chart comparing overlapping unemployment patterns across construction, finance, and manufacturing sectors from 2000-2010.
Area chart comparing overlapping unemployment patterns across construction, finance, and manufacturing sectors from 2000-2010 | Image by Author

Stacked Area Chart

Finally, we’ll create a stacked area chart to show total unemployment:

 
# 4. Stacked Area Chart

stacked_area = alt.Chart(selected_data).mark_area().encode(
    x=alt.X('date:T',
            title='Date',
            axis=alt.Axis(format='%Y')),
    y=alt.Y('count:Q',
            title='Number of Unemployed (Thousands)',
            stack=True),
    color=alt.Color('series:N',
                    title='Industry',
                    scale=alt.Scale(scheme='set2')),
    tooltip=[
        alt.Tooltip('date:T', title='Date', format='%b %Y'),
        alt.Tooltip('series:N', title='Industry'),
        alt.Tooltip('count:Q', title='Unemployed (Thousands)', format=','),
        alt.Tooltip('rate:Q', title='Industry Unemployment Rate', format='.1f')
    ]
)

stacked_area

This final visualization showcases:

  • stack=True: Enables stacking of areas (default behavior)
  • Cumulative view of total unemployment
  • Preserved individual industry coloring
  • Interactive tooltips for both total and industry-specific values

The stacked area chart effectively shows both the total unemployment across sectors and the relative contribution of each industry over time.

Stacked area chart showing combined unemployment totals across construction, finance, and manufacturing sectors from 2000-2010.
Stacked area chart showing combined unemployment totals across construction, finance, and manufacturing sectors from 2000-2010 | Image by Author

Conclusion

These examples demonstrate Altair’s versatility in creating area charts. From basic single-series visualizations to complex multi-series comparisons, Altair provides the tools needed to create insightful and interactive visualizations. The declarative nature of Altair’s API makes it straightforward to customize charts while maintaining clean, readable code.

Each type of area chart serves different analytical purposes:

  • Basic area charts are perfect for showing trends in a single series
  • Faceted charts allow independent analysis of multiple series
  • Overlapping areas highlight relationships between series
  • Stacked areas show both individual contributions and totals

Combined with interactive features like tooltips and proper formatting, these visualizations provide fantastic tools for exploring and presenting time-series data.

Leave a Reply

Your email address will not be published. Required fields are marked *