Skip to content

Commit 8885a0a

Browse files
committed
Improve docs
1 parent 6d4f085 commit 8885a0a

File tree

9 files changed

+99
-42
lines changed

9 files changed

+99
-42
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ repos:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer
77
- id: check-yaml
8+
args: [--unsafe]
89
- id: check-added-large-files
910
- id: check-toml
1011

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ uv add xarray_plotly
2424
```python
2525
import xarray as xr
2626
import numpy as np
27-
import xarray_plotly # registers the accessor
27+
from xarray_plotly import xpx
2828

2929
# Create sample data
3030
da = xr.DataArray(
@@ -40,13 +40,29 @@ da = xr.DataArray(
4040

4141
# Create an interactive line plot
4242
# Dimensions auto-assign: time→x, city→color, scenario→facet_col
43-
fig = da.plotly.line()
43+
fig = xpx(da).line()
4444
fig.show()
4545

4646
# Easy customization
4747
fig.update_layout(title="Temperature Projections", template="plotly_dark")
4848
```
4949

50+
## Usage Styles
51+
52+
xarray_plotly supports two equivalent usage styles:
53+
54+
```python
55+
# Function style (recommended) - full IDE code completion
56+
from xarray_plotly import xpx
57+
fig = xpx(da).line()
58+
59+
# Accessor style - works but no IDE completion
60+
import xarray_plotly
61+
fig = da.plotly.line()
62+
```
63+
64+
The `xpx()` function is recommended as it provides full IDE code completion and type hints.
65+
5066
## Features
5167

5268
- **Interactive plots**: Zoom, pan, hover for values, toggle traces
@@ -62,13 +78,13 @@ Dimensions are automatically assigned to plot "slots" based on their order:
6278
```python
6379
# dims: (time, city, scenario)
6480
# auto-assigns: time→x, city→color, scenario→facet_col
65-
da.plotly.line()
81+
xpx(da).line()
6682

6783
# Override with explicit assignments
68-
da.plotly.line(x="time", color="scenario", facet_col="city")
84+
xpx(da).line(x="time", color="scenario", facet_col="city")
6985

7086
# Skip a slot with None
71-
da.plotly.line(color=None) # time→x, city→facet_col
87+
xpx(da).line(color=None) # time→x, city→facet_col
7288
```
7389

7490
## Available Methods

docs/api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
# API Reference
22

3+
## xpx Function
4+
5+
The recommended way to use xarray_plotly with full IDE code completion:
6+
7+
```python
8+
from xarray_plotly import xpx
9+
10+
fig = xpx(da).line()
11+
```
12+
13+
::: xarray_plotly.xpx
14+
options:
15+
show_root_heading: true
16+
317
## Accessor
418

19+
The accessor style (`da.plotly.line()`) works but doesn't provide IDE code completion.
20+
521
::: xarray_plotly.accessor.DataArrayPlotlyAccessor
622
options:
723
show_root_heading: true

docs/examples/advanced.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"import pandas as pd\n",
2020
"import xarray as xr\n",
2121
"\n",
22-
"import xarray_plotly"
22+
"from xarray_plotly import xpx"
2323
]
2424
},
2525
{
@@ -57,7 +57,7 @@
5757
"da.coords[\"station\"].attrs = {\"long_name\": \"Measurement Station\"}\n",
5858
"\n",
5959
"# Labels are automatically extracted from attrs\n",
60-
"fig = da.plotly.line(title=\"Temperature with Auto-Labels\")\n",
60+
"fig = xpx(da).line(title=\"Temperature with Auto-Labels\")\n",
6161
"fig"
6262
]
6363
},
@@ -76,7 +76,7 @@
7676
"metadata": {},
7777
"outputs": [],
7878
"source": [
79-
"fig = da.plotly.line(\n",
79+
"fig = xpx(da).line(\n",
8080
" labels={\n",
8181
" \"temperature\": \"Temp (°C)\",\n",
8282
" \"time\": \"Date\",\n",
@@ -117,7 +117,7 @@
117117
")\n",
118118
"\n",
119119
"# Use line_dash for one dimension, color for another\n",
120-
"fig = da_complex.sel(city=\"NYC\").plotly.line(\n",
120+
"fig = xpx(da_complex.sel(city=\"NYC\")).line(\n",
121121
" color=\"scenario\",\n",
122122
" line_dash=\"model\",\n",
123123
" title=\"Multiple Visual Encodings\",\n",
@@ -141,7 +141,7 @@
141141
"outputs": [],
142142
"source": [
143143
"# Average over model dimension\n",
144-
"fig = da_complex.mean(\"model\").plotly.line(\n",
144+
"fig = xpx(da_complex.mean(\"model\")).line(\n",
145145
" facet_col=\"city\",\n",
146146
" title=\"Ensemble Mean by City\",\n",
147147
")\n",
@@ -155,7 +155,7 @@
155155
"outputs": [],
156156
"source": [
157157
"# Select a specific slice\n",
158-
"fig = da_complex.sel(scenario=\"SSP5\", model=\"GCM-A\").plotly.line(\n",
158+
"fig = xpx(da_complex.sel(scenario=\"SSP5\", model=\"GCM-A\")).line(\n",
159159
" facet_col=\"city\",\n",
160160
" title=\"SSP5 / GCM-A Projections\",\n",
161161
")\n",
@@ -184,7 +184,7 @@
184184
"source": [
185185
"da_simple = da.sel(station=\"Urban\")\n",
186186
"\n",
187-
"fig = da_simple.plotly.line(\n",
187+
"fig = xpx(da_simple).line(\n",
188188
" template=\"plotly_dark\",\n",
189189
" title=\"Dark Theme\",\n",
190190
")\n",
@@ -206,7 +206,7 @@
206206
"source": [
207207
"import plotly.express as px\n",
208208
"\n",
209-
"fig = da.plotly.line(\n",
209+
"fig = xpx(da).line(\n",
210210
" color_discrete_sequence=px.colors.qualitative.Set2,\n",
211211
" title=\"Custom Color Palette\",\n",
212212
")\n",
@@ -233,7 +233,7 @@
233233
")\n",
234234
"\n",
235235
"# Diverging colorscale centered at zero\n",
236-
"fig = da_2d.plotly.imshow(\n",
236+
"fig = xpx(da_2d).imshow(\n",
237237
" color_continuous_scale=\"RdBu_r\",\n",
238238
" color_continuous_midpoint=0,\n",
239239
" title=\"Diverging Colorscale\",\n",
@@ -256,7 +256,7 @@
256256
"metadata": {},
257257
"outputs": [],
258258
"source": [
259-
"fig = da.plotly.line()\n",
259+
"fig = xpx(da).line()\n",
260260
"\n",
261261
"# Add horizontal reference line\n",
262262
"fig.add_hline(y=15, line_dash=\"dash\", line_color=\"gray\", annotation_text=\"Reference\")\n",
@@ -289,7 +289,7 @@
289289
"metadata": {},
290290
"outputs": [],
291291
"source": [
292-
"fig = da.plotly.line()\n",
292+
"fig = xpx(da).line()\n",
293293
"\n",
294294
"# Make all lines thicker\n",
295295
"fig.update_traces(line_width=3)\n",
@@ -349,7 +349,7 @@
349349
"# Rolling mean\n",
350350
"da_smooth = da.rolling(time=7, center=True).mean()\n",
351351
"\n",
352-
"fig = da_smooth.plotly.line(\n",
352+
"fig = xpx(da_smooth).line(\n",
353353
" title=\"7-Day Rolling Mean\",\n",
354354
")\n",
355355
"fig"
@@ -374,7 +374,7 @@
374374
"\n",
375375
"monthly_mean = da_monthly.groupby(\"time.month\").mean()\n",
376376
"\n",
377-
"fig = monthly_mean.plotly.line(\n",
377+
"fig = xpx(monthly_mean).line(\n",
378378
" title=\"Monthly Climatology\",\n",
379379
")\n",
380380
"fig.update_xaxes(tickmode=\"array\", tickvals=list(range(1, 13)), \n",

docs/examples/plot-types.ipynb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"import pandas as pd\n",
2020
"import xarray as xr\n",
2121
"\n",
22-
"import xarray_plotly"
22+
"from xarray_plotly import xpx"
2323
]
2424
},
2525
{
@@ -88,7 +88,7 @@
8888
"metadata": {},
8989
"outputs": [],
9090
"source": [
91-
"fig = da_ts.plotly.line(title=\"Line Plot\")\n",
91+
"fig = xpx(da_ts).line(title=\"Line Plot\")\n",
9292
"fig"
9393
]
9494
},
@@ -105,7 +105,7 @@
105105
"metadata": {},
106106
"outputs": [],
107107
"source": [
108-
"fig = da_ts.plotly.line(markers=True, title=\"Line Plot with Markers\")\n",
108+
"fig = xpx(da_ts).line(markers=True, title=\"Line Plot with Markers\")\n",
109109
"fig"
110110
]
111111
},
@@ -124,7 +124,7 @@
124124
"metadata": {},
125125
"outputs": [],
126126
"source": [
127-
"fig = da_cat.plotly.bar(title=\"Stacked Bar Chart\")\n",
127+
"fig = xpx(da_cat).bar(title=\"Stacked Bar Chart\")\n",
128128
"fig"
129129
]
130130
},
@@ -141,7 +141,7 @@
141141
"metadata": {},
142142
"outputs": [],
143143
"source": [
144-
"fig = da_cat.plotly.bar(barmode=\"group\", title=\"Grouped Bar Chart\")\n",
144+
"fig = xpx(da_cat).bar(barmode=\"group\", title=\"Grouped Bar Chart\")\n",
145145
"fig"
146146
]
147147
},
@@ -171,7 +171,7 @@
171171
" name=\"energy\",\n",
172172
")\n",
173173
"\n",
174-
"fig = da_positive.plotly.area(title=\"Stacked Area Chart\")\n",
174+
"fig = xpx(da_positive).area(title=\"Stacked Area Chart\")\n",
175175
"fig"
176176
]
177177
},
@@ -190,7 +190,7 @@
190190
"metadata": {},
191191
"outputs": [],
192192
"source": [
193-
"fig = da_ts.plotly.scatter(title=\"Scatter Plot\")\n",
193+
"fig = xpx(da_ts).scatter(title=\"Scatter Plot\")\n",
194194
"fig"
195195
]
196196
},
@@ -209,7 +209,7 @@
209209
"metadata": {},
210210
"outputs": [],
211211
"source": [
212-
"fig = da_2d.plotly.scatter(x=\"lon\", y=\"lat\", color=\"value\", title=\"Lat/Lon Scatter\")\n",
212+
"fig = xpx(da_2d).scatter(x=\"lon\", y=\"lat\", color=\"value\", title=\"Lat/Lon Scatter\")\n",
213213
"fig"
214214
]
215215
},
@@ -236,7 +236,7 @@
236236
" name=\"response\",\n",
237237
")\n",
238238
"\n",
239-
"fig = da_dist.plotly.box(title=\"Box Plot\")\n",
239+
"fig = xpx(da_dist).box(title=\"Box Plot\")\n",
240240
"fig"
241241
]
242242
},
@@ -255,7 +255,7 @@
255255
"metadata": {},
256256
"outputs": [],
257257
"source": [
258-
"fig = da_2d.plotly.imshow(title=\"Heatmap\")\n",
258+
"fig = xpx(da_2d).imshow(title=\"Heatmap\")\n",
259259
"fig"
260260
]
261261
},
@@ -272,7 +272,7 @@
272272
"metadata": {},
273273
"outputs": [],
274274
"source": [
275-
"fig = da_2d.plotly.imshow(\n",
275+
"fig = xpx(da_2d).imshow(\n",
276276
" color_continuous_scale=\"Viridis\",\n",
277277
" title=\"Heatmap with Viridis colorscale\",\n",
278278
")\n",
@@ -306,7 +306,7 @@
306306
" name=\"value\",\n",
307307
")\n",
308308
"\n",
309-
"fig = da_3d.plotly.line(\n",
309+
"fig = xpx(da_3d).line(\n",
310310
" facet_col=\"city\",\n",
311311
" facet_row=\"scenario\",\n",
312312
" title=\"Faceted Line Plot\",\n",
@@ -341,7 +341,7 @@
341341
" name=\"sales\",\n",
342342
")\n",
343343
"\n",
344-
"fig = da_monthly.plotly.bar(\n",
344+
"fig = xpx(da_monthly).bar(\n",
345345
" x=\"product\",\n",
346346
" animation_frame=\"month\",\n",
347347
" title=\"Monthly Sales (Animated)\",\n",

docs/getting-started.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"source": [
3333
"## Basic Usage\n",
3434
"\n",
35-
"Import the package to register the `plotly` accessor:"
35+
"Import the `xpx` function for full IDE code completion:"
3636
]
3737
},
3838
{
@@ -45,7 +45,7 @@
4545
"import pandas as pd\n",
4646
"import xarray as xr\n",
4747
"\n",
48-
"import xarray_plotly # Registers the .plotly accessor"
48+
"from xarray_plotly import xpx # Provides full IDE code completion"
4949
]
5050
},
5151
{
@@ -97,7 +97,7 @@
9797
"outputs": [],
9898
"source": [
9999
"# Dimensions auto-assign: time→x, city→color, scenario→facet_col\n",
100-
"fig = da.plotly.line()\n",
100+
"fig = xpx(da).line()\n",
101101
"fig"
102102
]
103103
},
@@ -136,7 +136,7 @@
136136
"outputs": [],
137137
"source": [
138138
"# Put scenario on color, city on facets\n",
139-
"fig = da.plotly.line(color=\"scenario\", facet_col=\"city\")\n",
139+
"fig = xpx(da).line(color=\"scenario\", facet_col=\"city\")\n",
140140
"fig"
141141
]
142142
},
@@ -156,7 +156,7 @@
156156
"outputs": [],
157157
"source": [
158158
"# Skip color, so city goes to line_dash instead\n",
159-
"fig = da.sel(scenario=\"baseline\").plotly.line(color=None)\n",
159+
"fig = xpx(da.sel(scenario=\"baseline\")).line(color=None)\n",
160160
"fig"
161161
]
162162
},
@@ -175,7 +175,7 @@
175175
"metadata": {},
176176
"outputs": [],
177177
"source": [
178-
"fig = da.plotly.line()\n",
178+
"fig = xpx(da).line()\n",
179179
"\n",
180180
"fig.update_layout(\n",
181181
" title=\"Temperature Anomaly Projections\",\n",
@@ -199,7 +199,7 @@
199199
"metadata": {},
200200
"outputs": [],
201201
"source": [
202-
"fig = da.plotly.line(\n",
202+
"fig = xpx(da).line(\n",
203203
" title=\"Temperature Trends\",\n",
204204
" color_discrete_sequence=[\"#E63946\", \"#457B9D\", \"#2A9D8F\"],\n",
205205
" template=\"simple_white\",\n",

0 commit comments

Comments
 (0)