How to draw a stacked area chart in Altair

Altair, a Python library, facilitates declarative data visualization by offering a streamlined interface. It empowers users to craft interactive and insightful visual representations from their datasets. Altair leverages the capabilities of Vega and Vega-Lite, both visualization grammars engineered to simplify the creation of visuals with consistency and brevity. Additionally, Altair supports the creation of stacked area charts, adding another dimension to its range of visualization options.

Stacked area chart in Altair

A stacked area chart is a type of data visualization that represents multiple variables over a continuous axis, typically time. It displays the cumulative contribution of each variable, stacked on top of each other, resulting in a filled area chart. This visualization helps in understanding the total magnitude of the variables while also showing the relative contribution of each variable to the whole.

Stacked area chart example
Stacked area chart example

In Altair, mark_area() function creates stacked area charts where multiple datasets are visually represented as layers stacked on top of each other. This visualization method effectively displays the cumulative trend of each dataset over a continuous axis, such as time, facilitating easy comparison of their contributions to the whole.

Implementing stacked area chart

Here's an example illustrating how to draw a stacked area chart in Altair:

import altair as alt
import pandas as pd
import os
# Create example dataset
data = pd.DataFrame({
'time': pd.date_range('2024-01-01', periods=10),
'variable1': [3, 7, 5, 9, 8, 5, 6, 4, 7, 8],
'variable2': [2, 5, 6, 4, 5, 3, 7, 6, 4, 5],
'variable3': [1, 3, 2, 4, 3, 2, 5, 3, 2, 3]
})
# Melt the dataframe to long format for Altair
data_melted = pd.melt(data, id_vars=['time'], value_vars=['variable1', 'variable2', 'variable3'],
var_name='variable', value_name='value')
# Create Altair chart object
chart = alt.Chart(data_melted)
# Add layers for each variable
chart_area = chart.mark_area(opacity=0.7).encode(
x=alt.X('time:T', title='Time'),
y=alt.Y('value:Q', stack='normalize', title='Normalized Value'),
color=alt.Color('variable:N', title='Variable')
).properties(
width=600,
height=400,
title='Stacked Area Chart of Three Variables Over Time'
)
chart_area.save('chart.html')
os.system('cat chart.html')

Explanation

  • Lines 1–3: We import Altair and the necessary libraries.

  • Lines 5–11: We create a dummy DataFrame named data with three variables (variable1, variable2, variable3) and a time series ranging from 2024-01-01 to 2024-01-10.

  • Lines 13–15: We reshape the DataFrame from wide to long format using pd.melt(). This is necessary for Altair because it expects data in a long format for plotting. It stacks columns variable1, variable2, variable3 into a single column named variable and corresponding values into another column named value.

  • Lines 17–18: We initialize an Altair chart object with the melted DataFrame data_melted.

  • Lines 20–29: We define a stacked area chart named chart_area with the following specifications:

    • It uses the mark_area method to create the area chart, with opacity set to 0.7.

    • The encode method specifies how the data is mapped to visual properties:

      • The x-axis (x) represents time ('time:T') with a title Time.

      • The y-axis (y) represents the normalized values ('value:Q') with stacking set to normalize and a title Normalized Value. The y-axis values are normalized to compare the relative changes over time without being influenced by the absolute values of the variables, allowing for a better comparison of trends between the variables.

      • The color of the areas is determined by the variable ('variable:N') with a title Variable. The 'variable:N' specifies that the variable used for color encoding is categorical ('N' stands for nominal). This means that each unique value of the variable will be assigned a different color in the visualization.

    • The properties method sets additional chart properties:

      • Width of 600 pixels.

      • Height of 400 pixels.

      • Title of Stacked Area Chart of Three Variables Over Time.

  • Line 32: We save the chart using chart_area.save('chart.html'). It exports the chart to an HTML file named chart.html.

  • Line 33: We display the chart on the console.

Conclusion

Altair is a useful tool for data analysis and presentation and provides an interface for creating complex visualizations like stacked area charts. We can make use of the mark_area() function and other encoding methods to effectively visualize cumulative trends and compare the relative contributions of different variables over time.

Unlock your potential: Data visualization with the Altair series, all in one place!

To continue your exploration of data visualization using the Altair library, check out our series of Answers below:

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved