Skip to content

Commit 960e44e

Browse files
authored
Xarray support (#72)
* Added xarray support * small edits like adding titles and so on * Added da.xarray accessor * Added xarray to docs + bugfixes * formatting
1 parent e5ea75a commit 960e44e

11 files changed

Lines changed: 2604 additions & 78 deletions

File tree

‎README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,49 @@ canvas.show()
349349

350350
(<Figure size 590.551x324.803 with 1 Axes>,
351351
array([[<Axes: xlabel='x'>]], dtype=object))
352+
353+
### xarray data
354+
355+
Plot labelled [xarray](https://docs.xarray.dev) data directly
356+
(`pip install maxplotlibx[xarray]`). Axes come from the coordinates,
357+
labels from the `long_name` and `units` attributes, and titles from the
358+
coordinates you selected. `import maxplotlib.xarray` adds a `.maxplot`
359+
accessor that mirrors xarray’s own `.plot` API and returns an ordinary
360+
`Canvas`, so the backend is still chosen when rendering:
361+
362+
``` python
363+
import xarray as xr
364+
365+
import maxplotlib.xarray # registers da.maxplot and ds.maxplot
366+
367+
t = np.linspace(0, 1.5, 6)
368+
xs = np.linspace(0, 2 * np.pi, 80)
369+
ys = np.linspace(-1, 1, 50)
370+
wave = xr.DataArray(
371+
np.sin(xs - 2 * t[:, None, None]) * np.exp(-3 * ys[None, :, None] ** 2),
372+
dims=("t", "y", "x"),
373+
coords={"t": ("t", t, {"units": "s"}), "y": ys, "x": ("x", xs, {"units": "m"})},
374+
name="phi",
375+
attrs={"long_name": "Potential", "units": "V"},
376+
)
377+
378+
wave.maxplot.pcolormesh(col="t", col_wrap=3, canvas_kwargs={"width": "16cm", "ratio": 0.6}).show()
379+
```
380+
381+
![](README_files/figure-commonmark/cell-20-output-1.png)
382+
383+
(<Figure size 944.882x566.929 with 7 Axes>,
384+
array([[<Axes: title={'center': 't = 0 s'}, ylabel='y'>,
385+
<Axes: title={'center': 't = 0.3 s'}>,
386+
<Axes: title={'center': 't = 0.6 s'}>],
387+
[<Axes: title={'center': 't = 0.9 s'}, xlabel='x [m]', ylabel='y'>,
388+
<Axes: title={'center': 't = 1.2 s'}, xlabel='x [m]'>,
389+
<Axes: title={'center': 't = 1.5 s'}, xlabel='x [m]'>]],
390+
dtype=object))
391+
392+
The same works through Canvas methods,
393+
e.g. `canvas.plot(da, hue="species")`,
394+
`ax.pcolormesh(da, xcoord="R", ycoord="Z")` for curvilinear grids, or
395+
`Canvas.facet(da, col="t")`. `ds.maxplot.scatter(x=..., y=..., hue=...)`
396+
plots one Dataset variable against another. See the [xarray
397+
tutorial](tutorials/tutorial_17_xarray.ipynb) for more.

‎README.qmd‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,37 @@ Show all layers:
276276
```{python}
277277
canvas.show()
278278
```
279+
280+
### xarray data
281+
282+
Plot labelled [xarray](https://docs.xarray.dev) data directly
283+
(`pip install maxplotlibx[xarray]`). Axes come from the coordinates, labels
284+
from the `long_name` and `units` attributes, and titles from the coordinates
285+
you selected. `import maxplotlib.xarray` adds a `.maxplot` accessor that mirrors
286+
xarray's own `.plot` API and returns an ordinary `Canvas`, so the backend is
287+
still chosen when rendering:
288+
289+
```{python}
290+
import xarray as xr
291+
292+
import maxplotlib.xarray # registers da.maxplot and ds.maxplot
293+
294+
t = np.linspace(0, 1.5, 6)
295+
xs = np.linspace(0, 2 * np.pi, 80)
296+
ys = np.linspace(-1, 1, 50)
297+
wave = xr.DataArray(
298+
np.sin(xs - 2 * t[:, None, None]) * np.exp(-3 * ys[None, :, None] ** 2),
299+
dims=("t", "y", "x"),
300+
coords={"t": ("t", t, {"units": "s"}), "y": ys, "x": ("x", xs, {"units": "m"})},
301+
name="phi",
302+
attrs={"long_name": "Potential", "units": "V"},
303+
)
304+
305+
wave.maxplot.pcolormesh(col="t", col_wrap=3, canvas_kwargs={"width": "16cm", "ratio": 0.6}).show()
306+
```
307+
308+
The same works through Canvas methods, e.g. `canvas.plot(da, hue="species")`,
309+
`ax.pcolormesh(da, xcoord="R", ycoord="Z")` for curvilinear grids, or
310+
`Canvas.facet(da, col="t")`. `ds.maxplot.scatter(x=..., y=..., hue=...)` plots
311+
one Dataset variable against another. See the
312+
[xarray tutorial](tutorials/tutorial_17_xarray.ipynb) for more.
67.3 KB
Loading

‎docs/source/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ documentation for details.
3131
tutorials/tutorial_14_axis_and_layout_controls
3232
tutorials/tutorial_15_tikzfigure_subplots
3333
tutorials/tutorial_16_plotext_advanced
34+
tutorials/tutorial_17_xarray

‎pyproject.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ dependencies = [
2525
test = [
2626
"pytest",
2727
"coverage",
28+
"xarray",
29+
]
30+
xarray = [
31+
"xarray",
2832
]
2933
docs = [
34+
"xarray",
3035
"myst-parser",
3136
"sphinx",
3237
"sphinx-rtd-theme",

0 commit comments

Comments
 (0)