Una libreria per creare visualizzazioni narrative con Altair
Project description
pynarrative: Transform Data Visualizations into Stories
Overview
pynarrative is a Python library designed to transform data visualizations into engaging narratives. Built as an extension of Altair, pynarrative provides a powerful set of tools for creating interactive, narrative-driven visualizations that engage and inform your audience. The library bridges the gap between raw data visualization and storytelling by introducing narrative elements such as contextual descriptions, annotations, and interactive guidance through its main Story class and supporting NextStep functionality.
Architecture
pynarrative is built around two main classes: Story and NextStep. The Story class serves as the core component, handling the creation and management of narrative visualizations. It extends Altair's functionality while maintaining full compatibility with its declarative API. The NextStep class complements Story by providing specialized support for interactive elements, allowing you to guide users through your data narrative in various ways.
Installation
pynarrative can be easily installed using pip, Python's package manager. To install the library, run the following command in your terminal:
pip install pynarrative
The library requires Python 3.7 or later and depends on the following packages:
altair >= 4.0.0
pandas >= 1.0.0
These dependencies will be automatically installed if they're not already present in your environment.
Getting Started
PyNarrative is designed to be intuitive while providing powerful narrative capabilities. Here are two comprehensive examples showcasing both basic usage and advanced features:
Basic Example
import pynarrative as pn
import pandas as pd
# Sample data creation
data = pd.DataFrame({
'Year': range(2018, 2023),
'Sales': [100, 120, 90, 150, 200]
})
# Creating a complete chart in a single flow
final_chart = (pn.Story(data, width=600, height=400)
.mark_line(color='blue')
.encode(x='Year:O', y='Sales:Q')
.add_title(
"Sales Trends",
"2018-2022",
title_color="#1a1a1a",
subtitle_color="#4a4a4a"
)
.add_context(
"Steady Growth",
position='top',
color="#2ecc71"
)
.add_next_steps(
"Click for details",
position='bottom',
color="#3498db"
)
.add_annotation(
2020, 90, "Point of interest",
arrow_direction='left',
arrow_dx=50,
arrow_dy=-7,
arrow_color='red',
arrow_size=75,
label_color='darkgreen',
label_size=14,
show_point=True
)
.add_next_steps(
type='line_steps',
texts=["Phase 1", "Phase 2", "Phase 3", "Phase 4"],
position='bottom'
)
.add_next_steps(
type='button',
text="Click here",
url="https://example.com",
position='top',
title="Next steps"
)
.add_next_steps(
type='stair_steps',
texts=["Level 1", "Level 2", "Level 3", "Level 4", "Level 5"],
position='right'
)
.render()
)
Advanced Example with Customization
import pynarrative as pn
import pandas as pd
# Sample data creation
data = pd.DataFrame({
'Year': range(2018, 2023),
'Sales': [100, 120, 90, 150, 200]
})
# Creating a chart with advanced customizations
customized_chart = (pn.Story(data, width=600, height=400)
.mark_line(color='blue')
.encode(x='Year:O', y='Sales:Q')
.add_title(
"Sales Trends",
"2018-2022",
title_color="#1a1a1a",
subtitle_color="#4a4a4a"
)
.add_context(
"Steady Growth",
position='top',
color="#2ecc71"
)
.add_next_steps(
"Click for details",
position='bottom',
color="#3498db"
)
.add_annotation(
2020, 90, "Point of interest",
arrow_direction='left',
arrow_dx=50,
arrow_dy=-8,
arrow_color='red',
arrow_size=75,
label_color='darkgreen',
label_size=14,
show_point=True
)
# Customized linear steps
.add_next_steps(
type='line_steps',
texts=["Phase 1", "Phase 2", "Phase 3", "Phase 4"],
position='bottom',
line_steps_chart_width=350,
line_steps_chart_height=50,
line_steps_font_size=6
)
# Customized button
.add_next_steps(
type='button',
text="Click here",
url="https://example.com",
position='top',
title="Next steps",
title_color='blue',
title_font_family='Helvetica',
title_font_size=26,
button_width=150,
button_height=40,
button_color='#FF0000',
button_opacity=0.3
)
# Basic stair steps
.add_next_steps(
type='stair_steps',
texts=["Level 1", "Level 2", "Level 3", "Level 4"],
position='right'
)
.render()
)
These examples demonstrate how pynarrative allows you to create rich, interactive narrative visualizations with just a few lines of code. The first example illustrates the library's basic functionality, while the second shows how you can customize every aspect of the visualization in detail.
The Story Class
The Story class is the main interface for creating narrative visualizations. It extends Altair's functionality with methods specifically designed for storytelling. Through this class, you can add titles and subtitles, include explanatory text to provide context and insights, highlight specific data points with customizable arrows and labels, and integrate interactive guidance elements. All Story methods support method chaining, allowing for a fluid and intuitive API.
Documentation
Comprehensive documentation is available for pynarrative, providing detailed information about all features and capabilities. The documentation includes in-depth tutorials, complete API reference, and numerous examples showing different ways to use pynarrative for creating engaging data narratives.
The documentation is accessible on GitHub at:
Advanced Usage
pynarrative supports sophisticated narrative techniques through its advanced features, such as creating guided narratives with multiple interaction points. Annotations can be highly customized with different arrow styles, colors, and positioning. The library also provides flexible positioning systems for all narrative elements, allowing precise control over your story's visual hierarchy.
Contributing
pynarrative is an open-source project, and we welcome contributions from the community. Whether you're fixing bugs, adding new features, or improving documentation, your help is valuable. Please refer to our contribution guidelines in the repository for more information about how to get involved.
License
pynarrative is released under the MIT License, allowing both personal and commercial use with minimal restrictions. See the LICENSE file in the repository for the complete license text.
Authors
pynarrative was implemented by Roberto Olinto Barsotti as a master's thesis project in digital humanity, under the supervision of professor Angelica Lo Duca
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pynarrative-0.3.tar.gz.
File metadata
- Download URL: pynarrative-0.3.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b14299dec75238c21fc0d8901aac6127422ddd1e0d9304edebc1df200e5e46e
|
|
| MD5 |
6186ac31a216999e1e32c5da14426aed
|
|
| BLAKE2b-256 |
5d260700001341def3d454dbdd16300bb59603eb8e8e3089225f870f8dfde9a3
|
File details
Details for the file pynarrative-0.3-py3-none-any.whl.
File metadata
- Download URL: pynarrative-0.3-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13be5b0efe79ec7782fadac477cb68fea5e149cf880f9b055d591df1450c96e8
|
|
| MD5 |
d5d2e771bf71c2f367ba8238fbfdad46
|
|
| BLAKE2b-256 |
24328875cc73aa71b181ae5b2dfaef155111040b29ddeaf334ac86da2475cb92
|