

Photo by Author | Canva
We say that there are two people, person A and person B. You give them the same data to analyze them. But somehow, the story of A is better than B. Why? Because it is not just the data that matters. But how well you transform this data into a story that people can really understand. And let’s be real. Most of us are struggling with this section. We are logical. We are at a straight point. But tell the story? Not always our strong suit.
There are many libraries that you may have heard like metaplatlib, sea -borne, or ultrason that are widely used for data photography. But they most just pay attention to the drawing chart and they usually put more time and more lines of code. Therefore, they are better better than telling the story. But here’s the good news. Here is a new Azgar Library called Panterto, which makes the story easier. Can add captions, highlight key points, and guide your audience through data. This makes your reports and dashboards more engagements by producing results that really talk to readers. In this article, I will follow you how to use Panarto. We will cover the installation, how to make a statement, and eventually I will share some useful resources. So, let’s start:
to start with pynarrative
Installation and imports
To start, you will need some ordinary libraries (version 3.7 or later) and some ordinary libraries. Open your terminal and run the following command:
pip install pynarrative pandas altair
It will also install Panterto with its desired dependence (Pandas and Ultier). To keep things clean, you can also create a virtual environment. After installing, import the following libraries:
import pandas as pd
import pynarrative as pn
import altair as alt # Optional if you want to customize charts
The use of pynarrative to make the story
Once you get data, it is easy to make a statement chart. There is a class called story, named the story that wrapped around the ultrason chart. The basic flow for the construction of the story is:
- Create a pynarrative story: Transfer your data frame to pn.story, and describe the chart with ultra -encoding (such as mark_line (), encoded (), etc.).
- Add the statement elements: Series methods such as .add_title (), .add_context (), .add_annotation (), and .add_next_steps () to add text components.
- Take the story: Finally, to reveal the full narrative concept. Call on the render ().
Suppose you have data frame DF with column year and value. The way to tell the story around it is:
chart = (pn.Story(df, width=600, height=400)
.mark_line(color="steelblue")
.encode(x='Year:O', y='Value:Q')
.add_title("Yearly Trend", "2000-2020", title_color="#333")
.add_context("Values have increased over time", position='top')
.render())
chart
What does every part here do:
.add_title("Yearly Trend", "2000-2020")
: The plot holds an important title and sub -title..add_context("Values have increased...")
: Adds a descriptive note to the upper part of the chart..render()
: The final joint chart with all the statement elements.
You can also use .add_annotation()
To indicate a specified data point, or .add_next_steps()
To suggest operations (such as “Q4 Review” or link to more information).
First example: Covid-19 data
Let’s try a small example using Make -up COOD -19 Case count:
covid_df = pd.DataFrame({
'Month': ('Jan', 'Feb', 'Mar', 'Apr', 'May'),
'Cases': (1000, 3000, 7000, 5000, 2000)
})
# Create a narrative chart
covid_story = (pn.Story(covid_df)
.mark_line(color="firebrick")
.encode(x='Month:O', y='Cases:Q')
.add_title("COVID-19 Cases Over Time",
"Monthly trend",
title_color="#b22222")
.add_context("Cases peaked in March and declined in April/May", position='top')
.add_annotation('Mar', 7000, "Peak in March", arrow_color="gray", label_color="black")
.render())
covid_story
Output:
This code created a line of cases for a month. add_context
The call is written a phrase at the top (peak of march, then decrease), explaining the trend. add_annotation
The call is labeled on the March point (“Peak in March”), which points to this data point. Instead of just looking at the numbers on a graph, your audience now knows what happened and why it makes a difference. If you have to do the same thing using a simple ultering or metaphotleb, you need to manually detect the points and text spaces, which can get multiple lines of code.
Second example: unemployment data
Pynarrative works with any numerical data. For another example, let’s use public unemployment data:
unemp_df = pd.DataFrame({
'Year': (2018, 2019, 2020, 2021, 2022),
'UnemploymentRate': (4.5, 3.9, 8.1, 6.2, 5.3)
})
unemp_story = (pn.Story(unemp_df, width=600)
.mark_bar(color="teal")
.encode(x='Year:O', y='UnemploymentRate:Q')
.add_title("State Unemployment Rate", "2018-2022",
title_color="#333")
.add_context("Sharp increase in 2020 due to the pandemic", position='top')
.add_annotation(2020, 8.1, "Pandemic impact", arrow_color="red", label_color="darkred")
.render())
unemp_story
Output:
In this case, we use a bar chart to show the unemployment rate over time. The 2020 Spike is called directly, which makes the message clear that even anyone unfamiliar with the data.
Wrap and the next steps
You want to present the data and ensure the audience “get”. As you discover, check Official Panatoito Documents And examples. Start by installing and importing the library, then load your favorite public dataset with pandas (for example, CSV from Kagal or Data.gov) If you are new to programming Python.org Early Guide Or “or10 minutes of pandas“Lesson. With a small exercise, you will add clear, attractive stories to your data at some point.
Kanwal seals Kanwal is a machine learning engineer and a technical writer who has a deep passion for data science and has AI intersection with medicine. He authored EBook with “Maximum Production Capacity with Chat GPT”. As a Google Generation Scholar 2022 for the APAC, the Champions Diversity and the Educational Virtue. He is also recognized as a tech scholar, Mitacs Global Research Scholar, and Harvard Vacod Scholar as a Taradata diversity. Kanwal is a passionate lawyer for change, who has laid the foundation of a Fame Code to empower women in stem fields.