Skip to content

Commit fbd6e8d

Browse files
committed
Improve notebook
1 parent 963f5a2 commit fbd6e8d

File tree

1 file changed

+106
-128
lines changed

1 file changed

+106
-128
lines changed

docs/examples/fast_bar.ipynb

Lines changed: 106 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,34 @@
2626
]
2727
},
2828
{
29-
"cell_type": "code",
30-
"execution_count": null,
29+
"cell_type": "markdown",
3130
"id": "2",
3231
"metadata": {},
33-
"outputs": [],
3432
"source": [
35-
"# Sample data\n",
36-
"da = xr.DataArray(\n",
37-
" np.random.rand(12, 3) * 100,\n",
38-
" dims=[\"month\", \"category\"],\n",
39-
" coords={\n",
40-
" \"month\": [\n",
41-
" \"Jan\",\n",
42-
" \"Feb\",\n",
43-
" \"Mar\",\n",
44-
" \"Apr\",\n",
45-
" \"May\",\n",
46-
" \"Jun\",\n",
47-
" \"Jul\",\n",
48-
" \"Aug\",\n",
49-
" \"Sep\",\n",
50-
" \"Oct\",\n",
51-
" \"Nov\",\n",
52-
" \"Dec\",\n",
53-
" ],\n",
54-
" \"category\": [\"A\", \"B\", \"C\"],\n",
55-
" },\n",
56-
" name=\"sales\",\n",
57-
")"
33+
"## Basic Example"
5834
]
5935
},
6036
{
61-
"cell_type": "markdown",
37+
"cell_type": "code",
38+
"execution_count": null,
6239
"id": "3",
6340
"metadata": {},
41+
"outputs": [],
6442
"source": [
65-
"## Fast Bar Chart"
43+
"# Quarterly revenue data by product and region\n",
44+
"np.random.seed(42)\n",
45+
"da = xr.DataArray(\n",
46+
" np.random.rand(4, 3, 2) * 100 + 50,\n",
47+
" dims=[\"quarter\", \"product\", \"region\"],\n",
48+
" coords={\n",
49+
" \"quarter\": [\"Q1\", \"Q2\", \"Q3\", \"Q4\"],\n",
50+
" \"product\": [\"Widgets\", \"Gadgets\", \"Gizmos\"],\n",
51+
" \"region\": [\"North\", \"South\"],\n",
52+
" },\n",
53+
" name=\"revenue\",\n",
54+
")\n",
55+
"\n",
56+
"xpx(da).fast_bar()"
6657
]
6758
},
6859
{
@@ -72,15 +63,16 @@
7263
"metadata": {},
7364
"outputs": [],
7465
"source": [
75-
"xpx(da).fast_bar()"
66+
"# Comparison with regular bar()\n",
67+
"xpx(da).bar()"
7668
]
7769
},
7870
{
7971
"cell_type": "markdown",
8072
"id": "5",
8173
"metadata": {},
8274
"source": [
83-
"## Comparison with Regular Bar Chart"
75+
"## With Faceting"
8476
]
8577
},
8678
{
@@ -90,15 +82,15 @@
9082
"metadata": {},
9183
"outputs": [],
9284
"source": [
93-
"xpx(da).bar()"
85+
"xpx(da).fast_bar(facet_col=\"region\")"
9486
]
9587
},
9688
{
9789
"cell_type": "markdown",
9890
"id": "7",
9991
"metadata": {},
10092
"source": [
101-
"## Comparison with Area Chart"
93+
"## With Animation"
10294
]
10395
},
10496
{
@@ -108,17 +100,28 @@
108100
"metadata": {},
109101
"outputs": [],
110102
"source": [
111-
"xpx(da).area()"
103+
"# Multi-year data for animation\n",
104+
"np.random.seed(123)\n",
105+
"da_anim = xr.DataArray(\n",
106+
" np.random.rand(4, 3, 5) * 100 + 20,\n",
107+
" dims=[\"quarter\", \"product\", \"year\"],\n",
108+
" coords={\n",
109+
" \"quarter\": [\"Q1\", \"Q2\", \"Q3\", \"Q4\"],\n",
110+
" \"product\": [\"Widgets\", \"Gadgets\", \"Gizmos\"],\n",
111+
" \"year\": [2020, 2021, 2022, 2023, 2024],\n",
112+
" },\n",
113+
" name=\"revenue\",\n",
114+
")\n",
115+
"\n",
116+
"xpx(da_anim).fast_bar(animation_frame=\"year\")"
112117
]
113118
},
114119
{
115120
"cell_type": "markdown",
116121
"id": "9",
117122
"metadata": {},
118123
"source": [
119-
"## With Animation\n",
120-
"\n",
121-
"The `fast_bar` styling also applies to animation frames:"
124+
"## Faceting + Animation"
122125
]
123126
},
124127
{
@@ -128,179 +131,154 @@
128131
"metadata": {},
129132
"outputs": [],
130133
"source": [
131-
"# Data with animation dimension\n",
132-
"da_anim = xr.DataArray(\n",
133-
" np.random.rand(12, 3, 5) * 100,\n",
134-
" dims=[\"month\", \"category\", \"year\"],\n",
134+
"# 4D data: quarter x product x region x year\n",
135+
"np.random.seed(456)\n",
136+
"da_4d = xr.DataArray(\n",
137+
" np.random.rand(4, 3, 2, 4) * 80 + 30,\n",
138+
" dims=[\"quarter\", \"product\", \"region\", \"year\"],\n",
135139
" coords={\n",
136-
" \"month\": [\n",
137-
" \"Jan\",\n",
138-
" \"Feb\",\n",
139-
" \"Mar\",\n",
140-
" \"Apr\",\n",
141-
" \"May\",\n",
142-
" \"Jun\",\n",
143-
" \"Jul\",\n",
144-
" \"Aug\",\n",
145-
" \"Sep\",\n",
146-
" \"Oct\",\n",
147-
" \"Nov\",\n",
148-
" \"Dec\",\n",
149-
" ],\n",
150-
" \"category\": [\"A\", \"B\", \"C\"],\n",
151-
" \"year\": [2020, 2021, 2022, 2023, 2024],\n",
140+
" \"quarter\": [\"Q1\", \"Q2\", \"Q3\", \"Q4\"],\n",
141+
" \"product\": [\"Widgets\", \"Gadgets\", \"Gizmos\"],\n",
142+
" \"region\": [\"North\", \"South\"],\n",
143+
" \"year\": [2021, 2022, 2023, 2024],\n",
152144
" },\n",
153-
" name=\"sales\",\n",
145+
" name=\"revenue\",\n",
154146
")\n",
155147
"\n",
156-
"xpx(da_anim).fast_bar(animation_frame=\"year\")"
148+
"xpx(da_4d).fast_bar(facet_col=\"region\", animation_frame=\"year\")"
157149
]
158150
},
159151
{
160152
"cell_type": "markdown",
161153
"id": "11",
162154
"metadata": {},
163155
"source": [
164-
"## When to Use\n",
165-
"\n",
166-
"| Method | Use when... |\n",
167-
"|--------|-------------|\n",
168-
"| `fast_bar()` | Large datasets, animations with many frames, performance matters |\n",
169-
"| `bar()` | Need precise bar positioning, grouped bars, pattern fills |\n",
170-
"| `area()` | Want smooth continuous fills, standard area chart appearance |"
171-
]
172-
},
173-
{
174-
"cell_type": "markdown",
175-
"id": "12",
176-
"metadata": {},
177-
"source": [
178-
"## Negative Values\n",
156+
"## Positive and Negative Values\n",
179157
"\n",
180-
"Testing with all negative values:"
158+
"`fast_bar()` classifies each trace by its values:\n",
159+
"- **Purely positive** → stacks upward\n",
160+
"- **Purely negative** → stacks downward\n",
161+
"- **Mixed signs** → warning + dashed line (use `bar()` instead)"
181162
]
182163
},
183164
{
184165
"cell_type": "code",
185166
"execution_count": null,
186-
"id": "13",
167+
"id": "12",
187168
"metadata": {},
188169
"outputs": [],
189170
"source": [
190-
"# All negative values\n",
191-
"da_negative = xr.DataArray(\n",
192-
" -np.random.rand(6, 3) * 100,\n",
171+
"# Profit (positive) and Loss (negative) - stacks correctly\n",
172+
"np.random.seed(789)\n",
173+
"da_split = xr.DataArray(\n",
174+
" np.column_stack(\n",
175+
" [\n",
176+
" np.random.rand(6) * 80 + 20, # Revenue: positive\n",
177+
" -np.random.rand(6) * 50 - 10, # Costs: negative\n",
178+
" ]\n",
179+
" ),\n",
193180
" dims=[\"month\", \"category\"],\n",
194181
" coords={\n",
195182
" \"month\": [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\"],\n",
196-
" \"category\": [\"A\", \"B\", \"C\"],\n",
183+
" \"category\": [\"Revenue\", \"Costs\"],\n",
197184
" },\n",
198-
" name=\"loss\",\n",
185+
" name=\"financials\",\n",
199186
")\n",
200187
"\n",
201-
"xpx(da_negative).fast_bar()"
202-
]
203-
},
204-
{
205-
"cell_type": "code",
206-
"execution_count": null,
207-
"id": "14",
208-
"metadata": {},
209-
"outputs": [],
210-
"source": [
211-
"# Comparison: regular bar with negative values\n",
212-
"xpx(da_negative).bar()"
213-
]
214-
},
215-
{
216-
"cell_type": "markdown",
217-
"id": "15",
218-
"metadata": {},
219-
"source": [
220-
"## Mixed Values (Positive and Negative)\n",
221-
"\n",
222-
"`fast_bar()` classifies each trace (color group) by its values:\n",
223-
"- **Purely positive** → stacks upward (stackgroup='positive')\n",
224-
"- **Purely negative** → stacks downward (stackgroup='negative') \n",
225-
"- **Mixed signs** → shown as dashed line, no stacking\n",
226-
"\n",
227-
"When one column is all positive and another all negative, they stack correctly in separate groups:"
188+
"xpx(da_split).fast_bar()"
228189
]
229190
},
230191
{
231192
"cell_type": "code",
232193
"execution_count": null,
233-
"id": "16",
194+
"id": "13",
234195
"metadata": {},
235196
"outputs": [],
236197
"source": [
237-
"# Column A positive, Column B negative - stacks correctly\n",
238-
"da_split = xr.DataArray(\n",
239-
" np.array(\n",
198+
"# With animation - sign classification is consistent across frames\n",
199+
"np.random.seed(321)\n",
200+
"da_split_anim = xr.DataArray(\n",
201+
" np.stack(\n",
240202
" [\n",
241-
" [50, -20],\n",
242-
" [60, -40],\n",
243-
" [30, -30],\n",
244-
" [70, -25],\n",
245-
" [40, -35],\n",
246-
" [55, -15],\n",
247-
" ]\n",
203+
" np.column_stack([np.random.rand(6) * 80 + 20, -np.random.rand(6) * 50 - 10])\n",
204+
" for _ in range(4)\n",
205+
" ],\n",
206+
" axis=-1,\n",
248207
" ),\n",
249-
" dims=[\"month\", \"category\"],\n",
208+
" dims=[\"month\", \"category\", \"year\"],\n",
250209
" coords={\n",
251210
" \"month\": [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\"],\n",
252-
" \"category\": [\"Profit\", \"Loss\"],\n",
211+
" \"category\": [\"Revenue\", \"Costs\"],\n",
212+
" \"year\": [2021, 2022, 2023, 2024],\n",
253213
" },\n",
254214
" name=\"financials\",\n",
255215
")\n",
256216
"\n",
257-
"xpx(da_split).fast_bar()"
217+
"xpx(da_split_anim).fast_bar(animation_frame=\"year\")"
258218
]
259219
},
260220
{
261221
"cell_type": "markdown",
262-
"id": "17",
222+
"id": "14",
263223
"metadata": {},
264224
"source": [
265-
"Truly mixed columns (each has both + and -) are shown as dashed lines:"
225+
"## Mixed Sign Warning\n",
226+
"\n",
227+
"When a trace has both positive and negative values, `fast_bar()` shows a warning and displays it as a dashed line:"
266228
]
267229
},
268230
{
269231
"cell_type": "code",
270232
"execution_count": null,
271-
"id": "18",
233+
"id": "15",
272234
"metadata": {},
273235
"outputs": [],
274236
"source": [
275-
"# Truly mixed columns - shown as dashed lines (use bar() for these)\n",
237+
"# Both columns have mixed signs - triggers warning\n",
276238
"da_mixed = xr.DataArray(\n",
277239
" np.array(\n",
278240
" [\n",
279241
" [50, -30],\n",
280242
" [-40, 60],\n",
281243
" [30, -50],\n",
244+
" [-20, 40],\n",
282245
" ]\n",
283246
" ),\n",
284247
" dims=[\"month\", \"category\"],\n",
285248
" coords={\n",
286-
" \"month\": [\"Jan\", \"Feb\", \"Mar\"],\n",
249+
" \"month\": [\"Jan\", \"Feb\", \"Mar\", \"Apr\"],\n",
287250
" \"category\": [\"A\", \"B\"],\n",
288251
" },\n",
289252
")\n",
290253
"\n",
254+
"# This will show a warning\n",
291255
"xpx(da_mixed).fast_bar()"
292256
]
293257
},
294258
{
295259
"cell_type": "code",
296260
"execution_count": null,
297-
"id": "19",
261+
"id": "16",
298262
"metadata": {},
299263
"outputs": [],
300264
"source": [
301-
"# Comparison: regular bar with mixed values\n",
265+
"# For mixed data, use bar() instead\n",
302266
"xpx(da_mixed).bar()"
303267
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"id": "17",
272+
"metadata": {},
273+
"source": [
274+
"## When to Use\n",
275+
"\n",
276+
"| Method | Use when... |\n",
277+
"|--------|-------------|\n",
278+
"| `fast_bar()` | Large datasets, animations, performance matters, data is same-sign per trace |\n",
279+
"| `bar()` | Need grouped bars, pattern fills, or have mixed +/- values per trace |\n",
280+
"| `area()` | Want smooth continuous fills |"
281+
]
304282
}
305283
],
306284
"metadata": {

0 commit comments

Comments
 (0)