ColumnChart
This example, drawn from the Datawrapper documentation, demonstrates how to customize a column chart with annotations and custom colors.
import pandas as pd
import datawrapper as dw
# Load unemployment data from GitHub
url = "https://raw.githubusercontent.com/chekos/datawrapper/main/tests/samples/column/unemployment.csv"
df = pd.read_csv(url)
chart = dw.ColumnChart(
# Chart headline
title="U.S. unemployment rate",
# Introductory text
intro="January 2016-September 2020",
# Data source attribution
source_name="U.S. Bureau of Labor Statistics",
source_url="https://www.bls.gov/",
# Data from pandas DataFrame
data=df,
# Format labels with one decimal place and a percentage sign
y_grid_format=dw.NumberFormat.PERCENT_UP_TO_ONE_DECIMAL,
value_labels_format=dw.NumberFormat.PERCENT_UP_TO_ONE_DECIMAL,
# Highlight specific columns with custom colors
base_color="#CCCCCC",
color_category={
"2020/04": "rgb(21, 96, 122)",
"2020/05": "rgb(21, 96, 122)",
"2020/06": "rgb(21, 96, 122)",
"2020/07": "rgb(21, 96, 122)",
"2020/08": "rgb(21, 96, 122)",
"2020/09": "rgb(21, 96, 122)",
},
# Annotations to highlight the COVID-19 period
range_annotations=[
dw.RangeAnnotation(
x0="2020/01/01",
x1="2020/09/30",
color="#777777",
opacity=10,
type="x",
)
],
text_annotations=[
dw.TextAnnotation(
x="2020/04/01",
y=14,
dx=-50,
dy=50,
text="In <b>April 2020</b>, the unemployment rate rose to almost <b>15%</b>.",
align="tr",
size=14,
color="rgb(21, 96, 122)",
connector_line=dw.ConnectorLine(
color="rgb(21, 96, 122)",
type=dw.ConnectorLineType.CURVE_RIGHT,
),
)
],
)
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 |
|
30 |
The padding between bars as a percentage of the bar width |
int |
|
0 |
The base color for the chart (palette index or hex string) |
str or int |
|
“” |
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 |
|
“column-chart” |
The type of datawrapper chart to create |
Literal[column-chart] |
|
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 |
|
PydanticUndefined |
A list of columns to exclude from the color key |
list[str] |
|
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 |
|
PydanticUndefined |
A list of the highlighted series |
list[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 |
|
None |
The negative color to use, if you want one |
str or None |
|
“” |
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 |
|
“hover” |
Whether or not to show value labels |
ValueLabelDisplay (HOVER, ALWAYS, OFF) or str |
|
“” |
The source name that appears below the chart |
str |
|
“” |
The source URL that appears below the chart |
str |
|
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 |
|
PydanticUndefined |
Transform or dict[str, Any] |
|
|
“” |
How to format 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 |
|
“outside” |
Where to place the value labels |
ValueLabelPlacement (INSIDE, OUTSIDE, BELOW) or str |
|
“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 |
|
“outside” |
The labeling of the y grid labels |
GridLabelPosition (AUTO, INSIDE, OUTSIDE, …) or str |