Skip to content

Commit 79a7e8c

Browse files
committed
Add Dataset notebook
1 parent b63e637 commit 79a7e8c

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

docs/examples/datasets.ipynb

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Dataset Plotting\n",
8+
"\n",
9+
"Plot multiple variables from an xarray Dataset with automatic or custom slot assignment."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import numpy as np\n",
19+
"import xarray as xr\n",
20+
"\n",
21+
"from xarray_plotly import config, xpx\n",
22+
"\n",
23+
"config.notebook()"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# Create a Dataset with multiple variables\n",
33+
"time = np.arange(50)\n",
34+
"cities = [\"NYC\", \"LA\", \"Chicago\"]\n",
35+
"\n",
36+
"ds = xr.Dataset(\n",
37+
" {\n",
38+
" \"temperature\": ([\"time\", \"city\"], 20 + 5 * np.random.randn(50, 3).cumsum(axis=0) / 10),\n",
39+
" \"humidity\": ([\"time\", \"city\"], 50 + 10 * np.random.randn(50, 3).cumsum(axis=0) / 10),\n",
40+
" \"pressure\": ([\"time\", \"city\"], 1013 + np.random.randn(50, 3).cumsum(axis=0)),\n",
41+
" },\n",
42+
" coords={\"time\": time, \"city\": cities},\n",
43+
")\n",
44+
"ds"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Plot All Variables\n",
52+
"\n",
53+
"When you call a plot method on a Dataset without specifying `var`, all variables are combined into a single DataArray with a new `\"variable\"` dimension:"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"# All variables: time -> x, variable -> color, city -> line_dash\n",
63+
"xpx(ds).line()"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"## Control Where \"variable\" Goes\n",
71+
"\n",
72+
"The `\"variable\"` dimension can be assigned to any slot:"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"# Variables as facet columns\n",
82+
"xpx(ds).line(facet_col=\"variable\")"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"# Variables as rows, cities as columns\n",
92+
"xpx(ds).line(facet_row=\"variable\", facet_col=\"city\")"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"## Plot a Single Variable\n",
100+
"\n",
101+
"Use `var=\"name\"` to plot just one variable:"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"xpx(ds).line(var=\"temperature\", title=\"Temperature Only\")"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"## Different Plot Types"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"# Bar chart - latest values by city\n",
127+
"xpx(ds.isel(time=-1)).bar(x=\"city\", color=\"variable\", barmode=\"group\")"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"# Box plot - distribution by variable\n",
137+
"xpx(ds).box(x=\"variable\", color=\"city\")"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"# Area chart\n",
147+
"xpx(ds).area(var=\"humidity\", title=\"Humidity Over Time\")"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"# Scatter\n",
157+
"xpx(ds).scatter(var=\"temperature\", title=\"Temperature Scatter\")"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": null,
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"# Pie chart - snapshot at one time\n",
167+
"xpx(ds.isel(time=-1)).pie(var=\"temperature\", names=\"city\", title=\"Temperature Distribution\")"
168+
]
169+
},
170+
{
171+
"cell_type": "markdown",
172+
"metadata": {},
173+
"source": [
174+
"## Combining Slot Assignments\n",
175+
"\n",
176+
"Mix explicit assignments with auto-assignment:"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"# Explicit: variable -> facet_col, let city auto-assign to color\n",
186+
"xpx(ds).line(facet_col=\"variable\", color=\"city\")"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": null,
192+
"metadata": {},
193+
"outputs": [],
194+
"source": [
195+
"# Skip color slot with None\n",
196+
"xpx(ds).line(var=\"temperature\", color=None)"
197+
]
198+
}
199+
],
200+
"metadata": {
201+
"kernelspec": {
202+
"display_name": "Python 3",
203+
"language": "python",
204+
"name": "python3"
205+
},
206+
"language_info": {
207+
"name": "python",
208+
"version": "3.12.0"
209+
}
210+
},
211+
"nbformat": 4,
212+
"nbformat_minor": 4
213+
}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ nav:
6969
- Getting Started: getting-started.ipynb
7070
- Examples:
7171
- Plot Types: examples/plot-types.ipynb
72+
- Dataset Plotting: examples/datasets.ipynb
7273
- Dimensions & Facets: examples/dimensions.ipynb
7374
- Plotly Express Options: examples/kwargs.ipynb
7475
- Figure Customization: examples/figure.ipynb

0 commit comments

Comments
 (0)