BarChart

This example, drawn from the Datawrapper documentation, recreates a bar chart showing custom colors and a confidence interval using a bar overlay.

import pandas as pd
import datawrapper as dw

# Load data from GitHub
url = "https://raw.githubusercontent.com/chekos/datawrapper/main/tests/samples/bar/happiness-scores.csv"
df = pd.read_csv(url)

# Create the bar chart
chart = dw.BarChart(
    # Chart title with HTML formatting
    title="Score of Happiness<br>",
    # Source information
    source_name="World Happiness Report 2019",
    source_url="https://worldhappiness.report/ed/2019/changing-world-happiness/",
    # Intro text explaining the ranking methodology
    intro="The ranking is based on life evaluations (the average answer to the question how people evaluate the quality of their current lives on a scale of 0 to 10) for each country, averaged over the years 2016-2018.",
    # Byline
    byline="Daniela Haake",
    # Data
    data=df,
    # Value label format using enum
    value_label_format=dw.NumberFormat.THOUSANDS_WITH_OPTIONAL_DECIMALS,
    # Show value labels
    show_value_labels=True,
    # Value label alignment
    value_label_alignment="left",
    # Don't sort bars (keep original order)
    sort_bars=False,
    # Color by continent with custom mapping
    show_color_key=True,
    color_column="Continent",
    color_category={
        "Asia": "#ec6951",
        "Africa": "#8c1946",
        "Europe": "#ff9f51",
        "Oceania": "#e7d5c2",
        "3. Norway": "#f7d503",
        "7. Sweden": "#f7d503",
        "9. Canada": "#ef7f35",
        "93. China": "#c2c101",
        "1. Finland": "#f7d503",
        "140. India": "#c2c101",
        "147. Haiti": "#ef7f35",
        "149. Syria": "#c2c101",
        "151. Yemen": "#c2c101",
        "2. Denmark": "#f7d503",
        "4. Iceland": "#f7d503",
        "68. Russia": "#c2c101",
        "95. Bhutan": "#c2c101",
        "10. Austria": "#f7d503",
        "150. Malawi": "#2c7f67",
        "152. Rwanda": "#2c7f67",
        "17. Germany": "#f7d503",
        "148. Botswana": "#2c7f67",
        "153. Tanzania": "#2c7f67",
        "North America": "#e7d5c2",
        "5. Netherlands": "#f7d503",
        "6. Switzerland": "#f7d503",
        "8. New Zealand": "#ae5b3e",
        "154. Afghanistan": "#c2c101",
        "156. South Sudan": "#2c7f67",
        "19. United States": "#ef7f35",
        "15. United Kingdom": "#f7d503",
        "155. Central African Republic": "#2c7f67"
    },
    # Category labels for the legend
    label_column="Country",
    category_labels={
        "1. Finland": "Europe",
        "2. Denmark": "Europe",
        "3. Norway": "Europe",
        "4. Iceland": "Europe",
        "5. Netherlands": "Europe",
        "6. Switzerland": "Europe",
        "7. Sweden": "Europe",
        "8. New Zealand": "Oceania",
        "9. Canada": "North America",
        "10. Austria": "Europe",
        "15. United Kingdom": "Europe",
        "17. Germany": "Europe",
        "19. United States": "North America",
        "68. Russia": "Asia",
        "93. China": "Asia",
        "95. Bhutan": "Asia",
        "140. India": "Asia",
        "147. Haiti": "North America",
        "148. Botswana": "Africa",
        "149. Syria": "Asia",
        "150. Malawi": "Africa",
        "151. Yemen": "Asia",
        "152. Rwanda": "Africa",
        "153. Tanzania": "Africa",
        "154. Afghanistan": "Asia",
        "155. Central African Republic": "Africa",
        "156. South Sudan": "Africa"
    },
    # Bar overlay for confidence interval
    overlays=[
        dw.BarOverlay(
            from_column="Whisker-low",
            to_column="Whisker-high",
            color="#999999",
            opacity=0.7,
            pattern="diagonal-up",
            title="Confidence Interval (95%)",
            type="range",
            show_in_color_key=True,
            label_directly=False
        )
    ]
)

# Create the chart
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

axis_label_format

“”

The format of the axis 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

background

False

Fill the background of the bar’s full potential with a light color

bool

bar_column

“”

The column with the value for the bars

str

base_color

0

The default color for the chart (palette index or hex string)

str or int

block_labels

False

Whether to move labels to a separate line

bool

byline

“”

The byline that appears below the chart

str

category_labels

PydanticUndefined

Dictionary mapping category names to their display labels in the color legend

dict[str, str]

category_order

PydanticUndefined

List defining the order in which categories appear in the chart and legend

list[str]

chart_id

None

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

str or None

chart_type

“d3-bars”

The type of datawrapper chart to create

Literal[d3-bars]

color_category

PydanticUndefined

A mapping of layer names to colors

dict[str, str]

color_column

“”

The name of the column with the color for the bars

str

custom

PydanticUndefined

A dictionary of custom tags to attach to the chart

dict[str, Any]

custom_grid_lines

PydanticUndefined

Set custom grid lines

list[Any]

custom_range

PydanticUndefined

The custom range for the x axis

list[Any] or tuple[Any, 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]

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

exclude_from_color_key

PydanticUndefined

A list of column to exclude from the color key

list[str]

force_attribution

False

Whether to attribute the chart to datawrapper

bool

force_grid

False

Whether or not to show the x grid

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 bars

str or None

hide_title

False

Whether or not to hide the title

bool

highlighted_series

PydanticUndefined

A list of the highlighted series

list[str]

intro

“”

The intro text that appears above the chart

str

label_alignment

“left”

On which side are the labels aligned

Literal[left, right]

label_column

“”

The column with the labels for the bars

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

notes

“”

The footnotes that appear below the chart

str

overlays

PydanticUndefined

A list of bar overlays

list[UnionType[BarOverlay, dict[str, Any]]]

range_annotations

PydanticUndefined

A list of range annotations to display on the chart

Sequence[UnionType[RangeAnnotation, dict[Any, Any]]]

replace_flags

“off”

Whether to replace country codes with flag

ReplaceFlagsType (OFF, FOUR_BY_THREE, ONE_BY_ONE, …) or str

reverse_order

False

Whether to reverse the sort order

bool

rules

False

Draw a separating line between bars

bool

share_buttons

False

Whether to show social media share buttons

bool

share_url

“”

What URL to share

str

show_category_labels

True

Whether to show the value labels

bool

show_color_key

False

Whether to show the color key

bool

show_group_labels

True

Whether to show the group labels

bool

show_value_labels

True

Whether or not to show value labels with the bars

bool

sort_bars

False

Whether to sort the bars

bool

source_name

“”

The source name that appears below the chart

str

source_url

“”

The source URL that appears below the chart

str

stack_color_legend

False

Whether to stack the color key

bool

swap_labels

False

Whether to swap labels and values

bool

text_annotations

PydanticUndefined

A list of text annotations to display on the chart

Sequence[UnionType[TextAnnotation, dict[Any, Any]]]

theme

“datawrapper”

The theme of the chart

str

thick_bars

False

Make the bars thicker

bool

tick_position

“top”

The position of the ticks

Literal[top, bottom]

title

“”

The headline that appears above the chart

str

transformations

PydanticUndefined

Transform or dict[str, Any]

value_label_alignment

“left”

The alignment of the value labels

ValueLabelAlignment (LEFT, RIGHT) or str

value_label_format

“”

The format of the 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