Data Storytelling with Altair and pynarrative: Turning Data into Insight
Author: S Aishwarya
Originally published on Towards AI.
Strong data storytelling goes beyond simply visualizing numbers it uncovers the meaning behind the patterns, bringing clarity to what would otherwise be just a spreadsheet of values.
Photo by Carlos Muza on Unsplash
While visualization libraries like matplotlib, Plotly, and Seaborn can produce beautiful charts, they often lack one crucial feature: narrative. They leave it up to the viewer to interpret the story behind the lines and bars.
That’s where Altair and the pynarrative library shine. Together, they help us not only visualize data — but actually explain it.
What is Altair?
Altair is a Python library for declarative data visualization that allows users to create clean, concise, and interactive charts based on the Vega-Lite grammar of graphics.
You only need to provide:
your datachart typeencodingoptional interactivity, filtering, and tooltips
Altair then renders the visualization using a JSON specification — ready for use in dashboards, notebooks, web applications, or reports.
The Altair library directly integrates with pandasand Vega-Lite, making it easy for Python users to create powerful data stories without writing complex plotting code.
What is pynarrative?
pynarrative is a Python library designed to automatically craft clear, insightful narrative summaries from pandas DataFrames and Altair charts.
With just a few inputs:
A datasetA visualizationAxis labels, optional context, and your intended message
pynarrative generates a well-structured textual explanation — ideal for embedding in dashboards, reports, presentations, or interactive data stories.
Built to work seamlessly with pandasand Altair, pynarrative helps bridge the gap between raw data and human-readable insights — turning visualizations into compelling narratives with minimal effort.
Data Description:
We’re using the cars dataset, which contains information about different car models. The main features we’ll focus on are:
Horsepower: The power of the car’s engine.
Miles_per_Gallon: How fuel-efficient the car is.
Origin: Where the car was made.
Name: The model name of the car.
These features help us explore the relationship between a car’s power and fuel efficiency, and how that varies by origin.
Data Cleaning & Preparation
We’ll begin by automatically loading the dataset using Seaborn, then clean it for our visualizations.
import pandas as pdimport seaborn as snsimport altair as altimport pynarrative as pndf = sns.load_datasetprint)
Output:
Image by Author
Cleaning Steps:
Convert horsepower to numeric to handle any potential issues.
Drop rows with missing values in critical fields.
df= pd.to_numericdf_clean = df.dropnaprint)
Output:
Image by Author
Story 1: Power vs. Fuel Efficiency
Let’s explore the relationship between a car’s engine powerand its fuel efficiency.
By color-coding the data points based on the car’s region of origin, we gain insight into how different countries approach automotive design.
chart = pn.Story.mark_circle.encode.add_title.add_context.renderchart
Output:
Image by Author
This visualization reveals that American cars tend to have higher horsepower but lower fuel economy, whereas Japanese and European cars show more balance.
Story 2: Regional Efficiency Trends Over Time
Let’s observe how fuel efficiencyhas changed over time across different regions.
# Estimate year from model_year columndf_clean= df_clean+ 1900# Compute average MPG by region and yearregional_avg = df_clean.groupby.mean.reset_indexchart = pn.Story.mark_line.encode), y=alt.Y, color='origin:N').add_title.add_context.renderchart
Output:
Image by Author
We see how regulatory changes and fuel crises influenced fuel efficiency, especially in the U.S.
Story 3: Impact of the 1973 Oil Crisis
Let’s annotate our chart with the 1973 Oil Crisis, a pivotal moment for car design.
chart = pn.Story.mark_line.encode.add_title.add_context.add_annotation.renderchart
Output:
Image by Author
This annotated visualization adds historical context, showing how global events shape industry trends.
In Summary…
Using pynarrative and Altair, we seamlessly transformed car performance data into engaging visual stories by:
Highlighting the inverse relationship between horsepower and fuel efficiency
Exploring how regional design philosophies shape fuel economy over time
Annotating major historical events like the 1973 Oil Crisis to show their industry impact
All of this was done using a single, intuitive interface combining pandas, Altair, and pynarrative. Once this storytelling pipeline is in place, it can be adapted to any dataset rendered through Altair from automotive to healthcare and beyond.
This approach is quicker, more scalable, and more intuitive than conventional manual charting methods. Whether you’re building technical reports, dynamic dashboards, or insight-driven narratives, this serves as a reliable foundation for effective data storytelling.
I would love to read your comments!
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI
#data #storytelling #with #altair #pynarrative
Data Storytelling with Altair and pynarrative: Turning Data into Insight
Author: S Aishwarya
Originally published on Towards AI.
Strong data storytelling goes beyond simply visualizing numbers it uncovers the meaning behind the patterns, bringing clarity to what would otherwise be just a spreadsheet of values.
Photo by Carlos Muza on Unsplash
While visualization libraries like matplotlib, Plotly, and Seaborn can produce beautiful charts, they often lack one crucial feature: narrative. They leave it up to the viewer to interpret the story behind the lines and bars.
That’s where Altair and the pynarrative library shine. Together, they help us not only visualize data — but actually explain it.
🔍What is Altair?
Altair is a Python library for declarative data visualization that allows users to create clean, concise, and interactive charts based on the Vega-Lite grammar of graphics.
You only need to provide:
your datachart typeencodingoptional interactivity, filtering, and tooltips
Altair then renders the visualization using a JSON specification — ready for use in dashboards, notebooks, web applications, or reports.
The Altair library directly integrates with pandasand Vega-Lite, making it easy for Python users to create powerful data stories without writing complex plotting code.
🔍 What is pynarrative?
pynarrative is a Python library designed to automatically craft clear, insightful narrative summaries from pandas DataFrames and Altair charts.
With just a few inputs:
A datasetA visualizationAxis labels, optional context, and your intended message
pynarrative generates a well-structured textual explanation — ideal for embedding in dashboards, reports, presentations, or interactive data stories.
Built to work seamlessly with pandasand Altair, pynarrative helps bridge the gap between raw data and human-readable insights — turning visualizations into compelling narratives with minimal effort.
Data Description:
We’re using the cars dataset, which contains information about different car models. The main features we’ll focus on are:
Horsepower: The power of the car’s engine.
Miles_per_Gallon: How fuel-efficient the car is.
Origin: Where the car was made.
Name: The model name of the car.
These features help us explore the relationship between a car’s power and fuel efficiency, and how that varies by origin.
Data Cleaning & Preparation
We’ll begin by automatically loading the dataset using Seaborn, then clean it for our visualizations.
import pandas as pdimport seaborn as snsimport altair as altimport pynarrative as pndf = sns.load_datasetprint)
Output:
Image by Author
Cleaning Steps:
Convert horsepower to numeric to handle any potential issues.
Drop rows with missing values in critical fields.
df= pd.to_numericdf_clean = df.dropnaprint)
Output:
Image by Author
Story 1: Power vs. Fuel Efficiency
Let’s explore the relationship between a car’s engine powerand its fuel efficiency.
By color-coding the data points based on the car’s region of origin, we gain insight into how different countries approach automotive design.
chart = pn.Story.mark_circle.encode.add_title.add_context.renderchart
Output:
Image by Author
This visualization reveals that American cars tend to have higher horsepower but lower fuel economy, whereas Japanese and European cars show more balance.
Story 2: Regional Efficiency Trends Over Time
Let’s observe how fuel efficiencyhas changed over time across different regions.
# Estimate year from model_year columndf_clean= df_clean+ 1900# Compute average MPG by region and yearregional_avg = df_clean.groupby.mean.reset_indexchart = pn.Story.mark_line.encode), y=alt.Y, color='origin:N').add_title.add_context.renderchart
Output:
Image by Author
We see how regulatory changes and fuel crises influenced fuel efficiency, especially in the U.S.
Story 3: Impact of the 1973 Oil Crisis
Let’s annotate our chart with the 1973 Oil Crisis, a pivotal moment for car design.
chart = pn.Story.mark_line.encode.add_title.add_context.add_annotation.renderchart
Output:
Image by Author
This annotated visualization adds historical context, showing how global events shape industry trends.
In Summary…
Using pynarrative and Altair, we seamlessly transformed car performance data into engaging visual stories by:
Highlighting the inverse relationship between horsepower and fuel efficiency
Exploring how regional design philosophies shape fuel economy over time
Annotating major historical events like the 1973 Oil Crisis to show their industry impact
All of this was done using a single, intuitive interface combining pandas, Altair, and pynarrative. Once this storytelling pipeline is in place, it can be adapted to any dataset rendered through Altair from automotive to healthcare and beyond.
This approach is quicker, more scalable, and more intuitive than conventional manual charting methods. Whether you’re building technical reports, dynamic dashboards, or insight-driven narratives, this serves as a reliable foundation for effective data storytelling.
I would love to read your comments!
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI
#data #storytelling #with #altair #pynarrative
·195 Views