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

aria_description

“”

The alternative text for screen readers

str

auto_dark_mode

False

Whether the chart should automatically flip to dark mode when the user’s system is in dark mode

bool

base_color

0

The base color (can be hex string or palette index)

str or int

block_labels

False

Whether to use block labels

bool

byline

“”

The byline that appears below the chart

str

chart_id

None

The chart ID after creation (populated by create() method)

str or None

chart_type

“d3-bars-stacked”

The type of datawrapper chart to create

Literal[d3-bars-stacked]

color_category

PydanticUndefined

A mapping of layer names to colors

dict[str, str]

custom

PydanticUndefined

A dictionary of custom tags to attach to the chart

dict[str, Any]

dark_mode_invert

True

Whether to invert colors in dark mode

bool

data

PydanticUndefined

The data to use for the chart

DataFrame or list[dict]

date_label_format

“”

The date format. Use DateFormat enum for common formats or provide custom format strings.

DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or str

download_image

False

Whether to allow PNG download

bool

download_pdf

False

Whether to allow PDF download

bool

download_svg

False

Whether to allow SVG download

bool

embed

False

Whether to allow embedding

bool

force_attribution

False

Whether to attribute the chart to datawrapper

bool

forkable

True

Whether to allow other users to fork this visualization

bool

get_the_data

False

Whether to allow data downloads

bool

groups_column

None

The column to use for grouping

str or None

hide_title

False

Whether or not to hide the title

bool

intro

“”

The intro text that appears above the chart

str

language

“en-US”

The locale of the chart, which defines decimal and thousand separators as well as translations of month and weekday names.

str

logo

False

Whether to show a logo

bool

logo_id

“”

The id of the logo to show

str

negative_color

None

The negative color to use, if you want one

str or None

notes

“”

The footnotes that appear below the chart

str

range_value_labels

“”

The field you want to use for the value labels

str

replace_flags

“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

reverse_order

False

Reverse the order of the ranges

bool

share_buttons

False

Whether to show social media share buttons

bool

share_url

“”

What URL to share

str

show_color_key

False

Enables the legend

bool

sort_bars

False

Whether to sort bars

bool

sort_by

“”

Which column to sort by

str

sort_ranges

False

Whether to sort the ranges

bool

source_name

“”

The source name that appears below the chart

str

source_url

“”

The source URL that appears below the chart

str

stack_percentages

False

Whether to display values as percentages

bool

theme

“datawrapper”

The theme of the chart

str

thick_bars

False

Whether to use thick bars

bool

title

“”

The headline that appears above the chart

str

transformations

PydanticUndefined

Transform or dict[str, Any]

value_label_format

“”

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

value_label_mode

“left”

How to place the over-bar labels. Use ValueLabelMode enum for type safety or provide raw strings.

ValueLabelMode (LEFT, DIVERGING) or str