|
1 | 1 | # MultipleColumnChart |
2 | 2 |
|
3 | | -## Example |
4 | | - |
5 | | -This example demonstrates how to create a multiple column chart showing the population of the world's largest cities from 1950 to 2035. The chart uses 8 panels (one for each city) with custom HTML-styled titles, highlights the year 2025 in red, and includes text and range annotations. |
6 | | - |
7 | | -```python |
8 | | -import pandas as pd |
9 | | -import datawrapper as dw |
10 | | - |
11 | | -# Load data from GitHub |
12 | | -url = "https://raw.githubusercontent.com/palewire/datawrapper-api-classes/main/tests/samples/multiple_column/population.csv" |
13 | | -df = pd.read_csv(url) |
14 | | - |
15 | | -chart = dw.MultipleColumnChart( |
16 | | - # Chart metadata |
17 | | - title="Population of the world's largest cities, 1950 to 2035", |
18 | | - # Data source attribution |
19 | | - source_name="Our World in Data", |
20 | | - source_url="https://ourworldindata.org/urbanization", |
21 | | - # Data |
22 | | - data=df, |
23 | | - # Grid layout - 4 columns |
24 | | - grid_column=4, |
25 | | - # Fixed row height |
26 | | - grid_row_height=140, |
27 | | - # Y-axis format - abbreviated numbers (e.g., "10M") |
28 | | - y_grid_format=dw.NumberFormat.ABBREVIATED_ONE_DECIMAL, |
29 | | - # Y-axis labels inside the plot area |
30 | | - y_grid_labels="inside", |
31 | | - # X-axis and Y-axis grid lines off |
32 | | - x_grid_display="off", |
33 | | - y_grid_display="off", |
34 | | - # Base color for columns |
35 | | - base_color="#c9a291", |
36 | | - # Highlight 2025 in red |
37 | | - column_color={"2025": "#c71e1d"}, |
38 | | - # Category labels for legend |
39 | | - category_labels={"2025": "...for 2025"}, |
40 | | - # Exclude all other years from the legend |
41 | | - exclude_from_color_key=[ |
42 | | - "1950", "1955", "1960", "1965", "1970", "1975", "1980", "1985", |
43 | | - "1990", "1995", "2000", "2005", "2010", "2015", "2020", "2030", "2035" |
44 | | - ], |
45 | | - # Hide the color key |
46 | | - show_color_key=False, |
47 | | - # Panel titles with HTML styling |
48 | | - panel_titles={ |
49 | | - "Beijing": "Beijing, <span style=\"color:gray; font-weight: normal;\">China</span>", |
50 | | - "Delhi": "Delhi, <span style=\"color:gray; font-weight: normal;\">India</span>", |
51 | | - "Dhaka": "Dhaka, <span style=\"color:gray; font-weight: normal;\"> Bangladesh </span>", |
52 | | - "Lagos": "Lagos, <span style=\"color:gray; font-weight: normal;\">Nigeria</span>", |
53 | | - "Mumbai (Bombay)": "Mumbai, <span style=\"color:gray; font-weight: normal;\">India</span>", |
54 | | - "New York-Newark": "New York/Newark, <span style=\"color:gray; font-weight: normal;\">U.S.</span>", |
55 | | - "Paris": "Paris, <span style=\"color:gray; font-weight: normal;\">France</span>", |
56 | | - "Tokyo": "Tokyo, <span style=\"color:gray; font-weight: normal;\">Japan</span>", |
57 | | - }, |
58 | | - # Text annotations |
59 | | - text_annotations=[ |
60 | | - # Projection label for Mumbai |
61 | | - dw.TextAnnotation( |
62 | | - text="Projection", |
63 | | - x="2018/04/30 15:23", |
64 | | - y=36224085.8571, |
65 | | - plot="Mumbai (Bombay)", |
66 | | - dx=-21, |
67 | | - dy=-8, |
68 | | - align="tr", |
69 | | - size=13, |
70 | | - color="#858585", |
71 | | - connector_line=dw.ConnectorLine( |
72 | | - type=dw.ConnectorLineType.STRAIGHT, |
73 | | - stroke=dw.StrokeWidth.THIN |
74 | | - ) |
75 | | - ), |
76 | | - # Paris 2035 label |
77 | | - dw.TextAnnotation( |
78 | | - text="Paris 2035", |
79 | | - x="1947/07/03 01:00", |
80 | | - y=16409201.2857, |
81 | | - plot="Delhi", |
82 | | - align="tl", |
83 | | - size=13, |
84 | | - color="#858585" |
85 | | - ), |
86 | | - # Delhi 2025 population callout |
87 | | - dw.TextAnnotation( |
88 | | - text="<b style=\"color:#b73229\">2025 population:</b>\n34.7M", |
89 | | - x="2023/07/14 22:34", |
90 | | - y=33128010.1429, |
91 | | - plot="Delhi", |
92 | | - dx=-21, |
93 | | - dy=-29, |
94 | | - align="tr", |
95 | | - size=13, |
96 | | - color="#494949", |
97 | | - bg=True, |
98 | | - connector_line=dw.ConnectorLine( |
99 | | - type=dw.ConnectorLineType.STRAIGHT, |
100 | | - stroke=dw.StrokeWidth.THIN, |
101 | | - inherit_color=False |
102 | | - ) |
103 | | - ), |
104 | | - # Paris growth note |
105 | | - dw.TextAnnotation( |
106 | | - text="Paris grew the most between 1851 to 1856 (by over 20%).", |
107 | | - x="1947/10/03 18:05", |
108 | | - y=43097373.9429, |
109 | | - plot="Paris", |
110 | | - align="tl", |
111 | | - size=13, |
112 | | - color="#494949", |
113 | | - bg=True |
114 | | - ), |
115 | | - # Lagos projection note (hidden on mobile/desktop, shown as fallback) |
116 | | - dw.TextAnnotation( |
117 | | - text="Lagos is expected to double in size by 2035 compared to 2015.", |
118 | | - x="1947/07/03 01:00", |
119 | | - y=43096075.7522, |
120 | | - plot="Lagos", |
121 | | - align="tl", |
122 | | - size=13, |
123 | | - color="#494949", |
124 | | - bg=True, |
125 | | - show_mobile=False, |
126 | | - show_desktop=False |
127 | | - ), |
128 | | - ], |
129 | | - # Range annotations |
130 | | - range_annotations=[ |
131 | | - # Horizontal line in Paris panel |
132 | | - dw.RangeAnnotation( |
133 | | - x0="2009/01/23 14:50", |
134 | | - x1="2009/01/23 14:50", |
135 | | - y0=11765087.7143, |
136 | | - y1=13622733.1429, |
137 | | - plot="Paris", |
138 | | - range_type="y", |
139 | | - display="line", |
140 | | - color="#888", |
141 | | - opacity=76, |
142 | | - stroke_type="dotted", |
143 | | - stroke_width=2, |
144 | | - show_in_all_plots=True |
145 | | - ), |
146 | | - # Shaded projection period in Mumbai panel |
147 | | - dw.RangeAnnotation( |
148 | | - x0="2018/01/01 13:22", |
149 | | - x1="2037/07/02 01:00", |
150 | | - y0=26626251.1429, |
151 | | - y1=27245466.2857, |
152 | | - plot="Mumbai (Bombay)", |
153 | | - range_type="x", |
154 | | - display="range", |
155 | | - color="#888", |
156 | | - opacity=18, |
157 | | - show_in_all_plots=True |
158 | | - ), |
159 | | - ], |
160 | | -) |
161 | | - |
162 | | -chart.create() |
163 | | -``` |
164 | | - |
165 | | -<iframe title="Population of the world's largest cities, 1950 to 2035" aria-label="Multiple Columns" id="datawrapper-chart-ZEBmw" src="https://datawrapper.dwcdn.net/ZEBmw/1/" scrolling="no" frameborder="0" style="width: 0; min-width: 100% !important; border: none;" height="460" data-external="1"></iframe><script type="text/javascript">!function(){"use strict";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=0;r<e.length;r++)if(e[r].contentWindow===a.source){var i=a.data["datawrapper-height"][t]+"px";e[r].style.height=i}}}))}(); |
166 | | -</script> |
167 | | - |
168 | 3 | ## Reference |
169 | 4 |
|
170 | 5 | ```{eval-rst} |
|
0 commit comments