Skip to content

Commit 908ab32

Browse files
authored
Merge pull request #456 from palewire/main
Update ColumnChart example with improved annotations and data source
2 parents 7b4ce41 + 1530587 commit 908ab32

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

docs/user-guide/charts/column-charts.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# ColumnChart
22

3-
## Example
3+
This example, drawn from <a href="https://www.datawrapper.de/charts/column">the Datawrapper documentation</a>, demonstrates how to customize a column chart with annotations and custom colors.
44

5-
This example demonstrates how to create a column chart showing U.S. unemployment rates from 2016-2020, highlighting the dramatic spike during the COVID-19 pandemic.
5+
<iframe title="U.S. unemployment rate" aria-label="Column Chart" id="datawrapper-chart-smskc" src="https://datawrapper.dwcdn.net/smskc/1/" scrolling="no" frameborder="0" style="width: 0; min-width: 100% !important; border: none;" height="400" data-external="1"></iframe><script type="text/javascript">window.addEventListener("message",function(a){if(void 0!==a.data["datawrapper-height"]){var e=document.querySelectorAll("iframe");for(var t in a.data["datawrapper-height"])for(var r,i=0;r=e[i];i++)if(r.contentWindow===a.source){var d=a.data["datawrapper-height"][t]+"px";r.style.height=d}}});</script>
66

77
```python
88
import pandas as pd
99
import datawrapper as dw
1010

1111
# Load unemployment data from GitHub
12-
url = "https://raw.githubusercontent.com/palewire/datawrapper-api-classes/main/tests/samples/column/unemployment.csv"
12+
url = "https://raw.githubusercontent.com/chekos/datawrapper/main/tests/samples/column/unemployment.csv"
1313
df = pd.read_csv(url)
1414

1515
chart = dw.ColumnChart(
@@ -19,30 +19,53 @@ chart = dw.ColumnChart(
1919
source_name="U.S. Bureau of Labor Statistics",
2020
source_url="https://www.bls.gov/",
2121
# Introductory text
22-
intro="Seasonally adjusted unemployment rate, January 2016 - September 2020",
23-
# Chart byline
24-
byline="Sergio Hernandez",
22+
intro="January 2016-September 2020",
2523
# Data from pandas DataFrame
2624
data=df,
27-
# Format Y-axis grid labels with one decimal place
28-
y_grid_format=dw.NumberFormat.ONE_DECIMAL,
29-
# Always show value labels
30-
show_value_labels="always",
31-
# Place value labels outside the columns
32-
value_labels_placement="outside",
25+
# Format Y-axis grid labels with one decimal place and a percentage sign
26+
y_grid_format=dw.NumberFormat.PERCENT_UP_TO_ONE_DECIMAL,
3327
# Format value labels with one decimal place
34-
value_labels_format=dw.NumberFormat.ONE_DECIMAL,
35-
# Set fixed plot height in pixels
36-
plot_height=228,
28+
value_labels_format=dw.NumberFormat.PERCENT_UP_TO_ONE_DECIMAL,
3729
# Highlight specific column in red
38-
column_color={"2020/04": "#c71e1d"},
30+
base_color="#CCCCCC",
31+
color_category={
32+
"2020/04": "rgb(21, 96, 122)",
33+
"2020/05": "rgb(21, 96, 122)",
34+
"2020/06": "rgb(21, 96, 122)",
35+
"2020/07": "rgb(21, 96, 122)",
36+
"2020/08": "rgb(21, 96, 122)",
37+
"2020/09": "rgb(21, 96, 122)",
38+
},
39+
range_annotations=[
40+
dw.RangeAnnotation(
41+
x0="2020/01/01",
42+
x1="2020/09/30",
43+
color="#777777",
44+
opacity=10,
45+
type="x",
46+
)
47+
],
48+
text_annotations=[
49+
dw.TextAnnotation(
50+
x="2020/04/01",
51+
y=14,
52+
dx=-50,
53+
dy=50,
54+
text="In <b>April 2020</b>, the unemployment rate rose to almost <b>15%</b>.",
55+
align="tr",
56+
size=14,
57+
color="rgb(21, 96, 122)",
58+
connector_line=dw.ConnectorLine(
59+
color="rgb(21, 96, 122)",
60+
type=dw.ConnectorLineType.CURVE_RIGHT,
61+
),
62+
)
63+
],
3964
)
4065

4166
chart.create()
4267
```
4368

44-
<div style="min-height:433px" id="datawrapper-vis-1rJm1"><script type="text/javascript" defer src="https://datawrapper.dwcdn.net/1rJm1/embed.js" charset="utf-8" data-target="#datawrapper-vis-1rJm1"></script><noscript><img src="https://datawrapper.dwcdn.net/1rJm1/full.png" alt="" /></noscript></div>
45-
4669
## Reference
4770

4871
```{eval-rst}

0 commit comments

Comments
 (0)