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 |
|---|---|---|---|
|
“” |
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 |
|
“” |
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 |
|
False |
Fill the background of the bar’s full potential with a light color |
bool |
|
“” |
The column with the value for the bars |
str |
|
0 |
The default color for the chart (palette index or hex string) |
str or int |
|
False |
Whether to move labels to a separate line |
bool |
|
“” |
The byline that appears below the chart |
str |
|
PydanticUndefined |
Dictionary mapping category names to their display labels in the color legend |
dict[str, str] |
|
PydanticUndefined |
List defining the order in which categories appear in the chart and legend |
list[str] |
|
None |
The chart ID after creation (populated by create() method) |
str or None |
|
“d3-bars” |
The type of datawrapper chart to create |
Literal[d3-bars] |
|
PydanticUndefined |
A mapping of layer names to colors |
dict[str, str] |
|
“” |
The name of the column with the color for the bars |
str |
|
PydanticUndefined |
A dictionary of custom tags to attach to the chart |
dict[str, Any] |
|
PydanticUndefined |
Set custom grid lines |
list[Any] |
|
PydanticUndefined |
The custom range for the x axis |
list[Any] or tuple[Any, Any] |
|
True |
Whether to invert colors in dark mode |
bool |
|
PydanticUndefined |
The data to use for the chart |
DataFrame or list[dict] |
|
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 |
|
PydanticUndefined |
A list of column to exclude from the color key |
list[str] |
|
False |
Whether to attribute the chart to datawrapper |
bool |
|
False |
Whether or not to show the x grid |
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 bars |
str or None |
|
False |
Whether or not to hide the title |
bool |
|
PydanticUndefined |
A list of the highlighted series |
list[str] |
|
“” |
The intro text that appears above the chart |
str |
|
“left” |
On which side are the labels aligned |
Literal[left, right] |
|
“” |
The column with the labels for the bars |
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 |
|
“” |
The footnotes that appear below the chart |
str |
|
PydanticUndefined |
A list of bar overlays |
list[UnionType[BarOverlay, dict[str, Any]]] |
|
PydanticUndefined |
A list of range annotations to display on the chart |
Sequence[UnionType[RangeAnnotation, dict[Any, Any]]] |
|
“off” |
Whether to replace country codes with flag |
ReplaceFlagsType (OFF, FOUR_BY_THREE, ONE_BY_ONE, …) or str |
|
False |
Whether to reverse the sort order |
bool |
|
False |
Draw a separating line between bars |
bool |
|
False |
Whether to show social media share buttons |
bool |
|
“” |
What URL to share |
str |
|
True |
Whether to show the value labels |
bool |
|
False |
Whether to show the color key |
bool |
|
True |
Whether to show the group labels |
bool |
|
True |
Whether or not to show value labels with the bars |
bool |
|
False |
Whether to sort the bars |
bool |
|
“” |
The source name that appears below the chart |
str |
|
“” |
The source URL that appears below the chart |
str |
|
False |
Whether to stack the color key |
bool |
|
False |
Whether to swap labels and values |
bool |
|
PydanticUndefined |
A list of text annotations to display on the chart |
Sequence[UnionType[TextAnnotation, dict[Any, Any]]] |
|
“datawrapper” |
The theme of the chart |
str |
|
False |
Make the bars thicker |
bool |
|
“top” |
The position of the ticks |
Literal[top, bottom] |
|
“” |
The headline that appears above the chart |
str |
|
PydanticUndefined |
Transform or dict[str, Any] |
|
|
“left” |
The alignment of the value labels |
ValueLabelAlignment (LEFT, RIGHT) or str |
|
“” |
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 |