AreaChart
This example, drawn from Datawrapper’s official documentation, demonstrates how to create a stacked area chart. The chart includes text and range annotations to highlight significant events. It also customizes the color scheme for different regions and formats the axes and value labels for better readability.
import pandas as pd
import datawrapper as dw
# Load migration data from GitHub
df = pd.read_csv(
"https://raw.githubusercontent.com/chekos/Datawrapper/main/tests/samples/area/migration.csv"
)
chart = dw.AreaChart(
# The headline of the chart
title="Migration to the US by world region, 1820-2009",
# Introductory text explaining the data
intro="The numbers are recorded by decade. For example, the numbers recorded for 1905 tell us the number of immigrants between 1900 and 1910.",
# Data source attribution below the chart
source_name="Department of Homeland Security",
# URL to the original data source, linked in the chart's source attribution
source_url="http://metrocosm.com/wp-content/uploads/2016/05/usa-immigration-data.xlsx",
# Attribute of the author below the chart
byline="Mirko Lorenz",
# The DataFrame containing the source data
data=df,
# Transpose the data with a transformation
transformations=dict(transpose=True),
# Format x-axis labels as full years (e.g., "2020").
# Alternatively, you could provide "YYYY" if you'd rather not use the DateFormat enum.
x_grid_format=dw.DateFormat.YEAR_FULL,
# Format y-axis labels with abbreviated numbers (e.g., "1.2m")
# Alternatively, you could provide "0.[00]a" if you'd rather not use the NumberFormat enum.
y_grid_format=dw.NumberFormat.ABBREVIATED_TWO_DECIMALS,
# Align the y-axis labels to the right
y_grid_label_align="right",
# We always want the decimals for the tooltip number, but otherwise the same
tooltip_number_format="0.00a",
# Whether to stack the areas on top of each other
stack_areas=True,
# Use smooth curves between data points
interpolation=dw.LineInterpolation.CURVED,
# Set fixed plot height in pixels
plot_height_fixed=388,
# Map each region/country to a specific color
color_category={
"Austria-Hungary": "#1d81a2",
"Germany": "#004765",
"Ireland": "#3a96b8",
"Italy": "#48adc0",
"Norway and Sweden": "#2b8589",
"Russia": "#329a9b",
"United Kingdom": "#257085",
"Rest of Europe": "#005d71",
"Europe": "#00658d",
"China": "#b4241c",
"India": "#cd3d2e",
"Philippines": "#dc464b",
"Rest of Asia": "#E65340",
"Asia": "#e65341",
"Canada and Newfoundland": "#ffa126",
"Mexico": "#ffca76",
"Caribbean": "#ffe59c",
"Central America": "#ffdc6b",
"South America": "#ffbb7f",
"Rest of America": "#fffbb1",
"Africa": "#009a69",
"Oceania": "#003f65",
"Not Specified": "#181818",
},
# Text annotations to label regions and events
text_annotations=[
dw.TextAnnotation(
text="E U R O P E",
x="1896/07/27 16:00",
y=1329923.3141,
align="mc",
color="#fefefe",
bold=True,
size=20,
outline=False,
),
dw.TextAnnotation(
text="A S I A ",
x="2000M5",
y=2500000,
align="mr",
color="#121212",
bold=True,
size=14,
outline=False,
),
dw.TextAnnotation(
text="A M E -\nR I C A",
x="1994/08/01 20:41",
y=6134172.0713,
align="mc",
color="#222222",
bold=True,
size=10,
outline=False,
),
dw.TextAnnotation(
text="Germany",
x="1851/01/04 01:21",
y=439746.2628,
align="ml",
color="#d2d2d2",
italic=True,
size=11,
outline=False,
),
dw.TextAnnotation(
text="World <br/>War I",
x="1914M07",
y=8000000.000000002,
align="tl",
dx=3,
color="#575757",
italic=True,
size=11,
outline=False,
),
dw.TextAnnotation(
text="World <br/>War II",
x="1939M09",
y=3000000.000000002,
align="tl",
dx=3,
color="#575757",
italic=True,
size=11,
outline=False,
),
],
range_annotations=[
# World War I period (1914-1918)
dw.RangeAnnotation(
x0="1914M07",
x1="1918M11",
color="#333333",
opacity=14,
),
# World War II period (1939-1945)
dw.RangeAnnotation(
x0="1939M09",
x1="1945M09",
color="#333333",
opacity=10,
),
],
)
# Create the chart in Datawrapper
chart.create()
Reference
Parameter |
Default |
Description |
Type |
|---|---|---|---|
|
0.8 |
The opacity of the areas |
float |
|
“#4682b4” |
The color of the separator lines between areas |
str or int |
|
False |
Whether or not to show separator lines between areas |
bool |
|
“” |
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 for layers (palette index or hex string) |
str or int |
|
“” |
The byline that appears below the chart |
str |
|
None |
The chart ID after creation (populated by create() method) |
str or None |
|
“d3-area” |
The type of datawrapper chart to create |
Literal[d3-area] |
|
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] |
|
None |
Custom range for X-axis as [min, max]. Overrides automatic range calculation. |
list[Any] or tuple[Any, Any] or None |
|
None |
Custom range for Y-axis as [min, max]. Overrides automatic range calculation. |
list[Any] or tuple[Any, Any] or None |
|
None |
Custom tick mark positions for X-axis. List of values where ticks should appear. |
list[Any] or None |
|
None |
Custom tick mark positions for Y-axis. List of values where ticks should appear. |
list[Any] or None |
|
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 |
|
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 |
|
False |
Whether or not to hide the title |
bool |
|
“linear” |
The interpolation method to use when drawing lines |
LineInterpolation (LINEAR, STEP, STEP_AFTER, …) or str |
|
“” |
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 |
|
“” |
The footnotes that appear below the chart |
str |
|
300 |
The fixed height of the plot |
int or float |
|
“fixed” |
How to set the plot height |
PlotHeightMode (FIXED, RATIO) or str |
|
0.5 |
The ratio of the plot height |
float |
|
PydanticUndefined |
A list of range annotations to display on the chart |
Sequence[UnionType[RangeAnnotation, dict[Any, Any]]] |
|
False |
Whether to show social media share buttons |
bool |
|
“” |
What URL to share |
str |
|
False |
Whether or not to show the color key above the chart |
bool |
|
True |
Whether or not to show tooltips on hover |
bool |
|
“keep” |
How to sort area layers |
Literal[keep, asc, desc] |
|
“” |
The source name that appears below the chart |
str |
|
“” |
The source URL that appears below the chart |
str |
|
False |
Whether or not to stack areas |
bool |
|
False |
Whether or not to stack areas to 100% |
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 |
|
“” |
The headline that appears above the chart |
str |
|
“” |
The format of the number tooltip. 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 |
|
“” |
The format for the x-axis tooltips. 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 |
|
PydanticUndefined |
Transform or dict[str, Any] |
|
|
“off” |
X-axis grid display setting. Controls vertical grid lines. |
GridDisplay (OFF, ON, TICKS, …) or str or bool or None |
|
None |
Format string for X-axis grid labels. Supports date and number formats. |
DateFormat (AUTO, YEAR_FULL, YEAR_TWO_DIGIT, …) or NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str or None |
|
“on” |
Y-axis grid display setting. Controls horizontal grid lines. |
GridDisplay (OFF, ON, TICKS, …) or str or bool or None |
|
None |
Format string for Y-axis grid labels. Supports number formats. |
NumberFormat (AUTO, THOUSANDS_WITH_OPTIONAL_DECIMALS, INTEGER, …) or str or None |
|
“left” |
Which side to put the y-axis labels on |
GridLabelAlign (LEFT, RIGHT) or str |
|
“auto” |
The labeling of the y grid labels |
GridLabelPosition (AUTO, INSIDE, OUTSIDE, …) or str |