StackedBarChart
This example, drawn from Datawrapper’s official documentation, demonstrates how to create a diverging stacked bar chart showing trust levels in media reporting across different topics. The chart displays percentages with a custom color scheme to differentiate between trust levels, sorted by low trust values. It also includes value formatting.
import pandas as pd
import datawrapper as dw
# Load media trust data from GitHub
df = pd.read_csv(
"https://raw.githubusercontent.com/chekos/Datawrapper/main/tests/samples/stacked_bar/media-trust.csv",
sep=";"
)
chart = dw.StackedBarChart(
# Chart title
title="Trust in Media Reporting",
# Introductory text explaining the context
intro="Trust in Media Reporting regarding widely reported topics of 2015",
# Data source attribution
source_name="Infratest dimap",
source_url="http://www.infratest-dimap.de/umfragen-analysen/bundesweit/umfragen/aktuell/wenig-vertrauen-in-medienberichterstattung/",
# Data from pandas DataFrame
data=df,
# Enable percentage stacking
stack_percentages=True,
# Format value labels as percentages
value_label_format="0%",
# Use diverging mode for better visual separation
value_label_mode="diverging",
# Sort bars by the "Low trust" column in descending order
sort_bars=True,
sort_by="Low trust",
reverse_order=True,
# Enable the color legend
show_color_key=True,
# Use thick bars for better visibility
thick_bars=True,
# Use block labels for category names
block_labels=True,
# Custom color mapping for each trust level
color_category={
"Very high trust": "#15607a",
"High trust": "#719aae",
"No answer": "#e8e8e8",
"Low trust": "#ff6954",
"Very low trust": "#c71e1d",
},
)
# Create the chart in Datawrapper
chart.create()
Reference
Parameter |
Default |
Description |
Type |
|---|---|---|---|
|
“” |
The alternative text for screen readers |
str |
|
False |
Whether the chart should automatically flip to dark mode when the user’s system is in dark mode |
bool |
|
0 |
The base color (can be hex string or palette index) |
str or int |
|
False |
Whether to use block labels |
bool |
|
“” |
The byline that appears below the chart |
str |
|
None |
The chart ID after creation (populated by create() method) |
str or None |
|
“d3-bars-stacked” |
The type of datawrapper chart to create |
Literal[d3-bars-stacked] |
|
PydanticUndefined |
A mapping of layer names to colors |
dict[str, str] |
|
PydanticUndefined |
A dictionary of custom tags to attach to the chart |
dict[str, Any] |
|
True |
Whether to invert colors in dark mode |
bool |
|
PydanticUndefined |
The data to use for the chart |
DataFrame or list[dict] |
|
“” |
The date format. Use DateFormat enum for common formats or provide custom format strings. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or str |
|
False |
Whether to allow PNG download |
bool |
|
False |
Whether to allow PDF download |
bool |
|
False |
Whether to allow SVG download |
bool |
|
False |
Whether to allow embedding |
bool |
|
False |
Whether to attribute the chart to datawrapper |
bool |
|
True |
Whether to allow other users to fork this visualization |
bool |
|
False |
Whether to allow data downloads |
bool |
|
None |
The column to use for grouping |
str or None |
|
False |
Whether or not to hide the title |
bool |
|
“” |
The intro text that appears above the chart |
str |
|
“en-US” |
The locale of the chart, which defines decimal and thousand separators as well as translations of month and weekday names. |
str |
|
False |
Whether to show a logo |
bool |
|
“” |
The id of the logo to show |
str |
|
None |
The negative color to use, if you want one |
str or None |
|
“” |
The footnotes that appear below the chart |
str |
|
“” |
The field you want to use for the value labels |
str |
|
“off” |
Whether to replace country codes with flags. Use ReplaceFlagsType enum for type safety or provide raw strings. |
ReplaceFlagsType (OFF, FOUR_BY_THREE, ONE_BY_ONE, …) or str |
|
False |
Reverse the order of the ranges |
bool |
|
False |
Whether to show social media share buttons |
bool |
|
“” |
What URL to share |
str |
|
False |
Enables the legend |
bool |
|
False |
Whether to sort bars |
bool |
|
“” |
Which column to sort by |
str |
|
False |
Whether to sort the ranges |
bool |
|
“” |
The source name that appears below the chart |
str |
|
“” |
The source URL that appears below the chart |
str |
|
False |
Whether to display values as percentages |
bool |
|
“datawrapper” |
The theme of the chart |
str |
|
False |
Whether to use thick bars |
bool |
|
“” |
The headline that appears above the chart |
str |
|
PydanticUndefined |
Transform or dict[str, Any] |
|
|
“” |
The number format for value labels. Use DateFormat for temporal data, NumberFormat for numeric data, or provide custom format strings. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str |
|
“left” |
How to place the over-bar labels. Use ValueLabelMode enum for type safety or provide raw strings. |
ValueLabelMode (LEFT, DIVERGING) or str |