|
2 | 2 |
|
3 | 3 | **Interactive Plotly Express plotting accessor for xarray** |
4 | 4 |
|
5 | | -xarray_plotly provides interactive plotting for xarray DataArray objects using Plotly Express. It automatically assigns dimensions to plot slots based on their order, making it easy to create rich, interactive visualizations with minimal code. |
| 5 | +::: xarray_plotly |
| 6 | + options: |
| 7 | + show_root_heading: false |
| 8 | + show_docstring_description: true |
| 9 | + show_docstring_examples: true |
| 10 | + members: false |
6 | 11 |
|
7 | | -## Features |
| 12 | +## Installation |
8 | 13 |
|
9 | | -- **Interactive plots**: Zoom, pan, hover for values, toggle traces - all built-in |
10 | | -- **Automatic dimension assignment**: Dimensions fill plot slots (x, color, facet_col, etc.) by position |
11 | | -- **Easy customization**: Returns Plotly `Figure` objects for further modification |
12 | | -- **Multiple plot types**: Line, bar, area, scatter, box, and heatmap plots |
13 | | -- **Faceting and animation**: Built-in support for subplot grids and animated time series |
14 | | -- **Full IDE support**: The `xpx()` function provides complete code completion and type hints |
| 14 | +```bash |
| 15 | +pip install xarray_plotly |
| 16 | +``` |
15 | 17 |
|
16 | | -## Quick Example |
| 18 | +## Usage |
17 | 19 |
|
18 | 20 | ```python |
19 | 21 | import xarray as xr |
20 | 22 | import numpy as np |
21 | | -from xarray_plotly import xpx |
| 23 | +import xarray_plotly # registers the accessor |
22 | 24 |
|
23 | | -# Create sample data |
24 | 25 | da = xr.DataArray( |
25 | | - np.random.randn(100, 3, 2), |
26 | | - dims=["time", "city", "scenario"], |
27 | | - coords={ |
28 | | - "time": np.arange(100), |
29 | | - "city": ["NYC", "LA", "Chicago"], |
30 | | - "scenario": ["baseline", "warming"], |
31 | | - }, |
32 | | - name="temperature", |
| 26 | + np.random.randn(100, 3).cumsum(axis=0), |
| 27 | + dims=["time", "city"], |
| 28 | + coords={"time": np.arange(100), "city": ["NYC", "LA", "Chicago"]}, |
33 | 29 | ) |
34 | 30 |
|
35 | | -# Create an interactive line plot |
36 | | -# Dimensions auto-assign: time->x, city->color, scenario->facet_col |
37 | | -fig = xpx(da).line() |
38 | | -fig.show() |
39 | | - |
40 | | -# Easy customization |
41 | | -fig.update_layout( |
42 | | - title="Temperature Projections", |
43 | | - template="plotly_dark", |
44 | | -) |
45 | | -``` |
46 | | - |
47 | | -## Usage Styles |
48 | | - |
49 | | -xarray_plotly supports two equivalent usage styles: |
| 31 | +# Accessor style |
| 32 | +fig = da.plotly.line() |
50 | 33 |
|
51 | | -```python |
52 | | -# Function style (recommended) - full IDE code completion |
| 34 | +# Or with xpx() for IDE code completion |
53 | 35 | from xarray_plotly import xpx |
54 | 36 | fig = xpx(da).line() |
55 | | - |
56 | | -# Accessor style - works but no IDE completion |
57 | | -import xarray_plotly |
58 | | -fig = da.plotly.line() |
59 | 37 | ``` |
60 | 38 |
|
61 | | -The `xpx()` function is recommended as it provides full IDE code completion and type hints. |
62 | | - |
63 | | -**Why no IDE completion for the accessor?** xarray accessors are registered dynamically at runtime using `register_dataarray_accessor()`. Python's static type checkers and IDEs cannot see these dynamically added attributes, so `da.plotly` appears as an unknown attribute. This limitation could only be solved by xarray itself (e.g., through a type checker plugin), if at all. The `xpx()` function provides a workaround with an explicit return type that IDEs can follow. |
64 | | - |
65 | | -## Installation |
66 | | - |
67 | | -```bash |
68 | | -pip install xarray_plotly |
69 | | -``` |
70 | | - |
71 | | -Or with uv: |
72 | | - |
73 | | -```bash |
74 | | -uv add xarray_plotly |
75 | | -``` |
76 | | - |
77 | | -## Why xarray_plotly? |
78 | | - |
79 | | -The current `.plot` accessor in xarray is built on matplotlib, which has limitations for modern data exploration: |
80 | | - |
81 | | -1. **Static outputs**: Matplotlib plots are non-interactive |
82 | | -2. **Post-creation modification is cumbersome**: Requires understanding complex object hierarchies |
83 | | -3. **Multi-dimensional data**: No built-in support for faceting or animation |
84 | | - |
85 | | -xarray_plotly solves these with Plotly Express, providing: |
| 39 | +## Next Steps |
86 | 40 |
|
87 | | -- Interactive plots with zero additional code |
88 | | -- Simple, predictable dimension-to-slot assignment |
89 | | -- Easy post-creation customization via Plotly's `Figure` API |
90 | | -- Modern visualization patterns (faceting, animation) built-in |
| 41 | +- [Getting Started](getting-started.ipynb) - Interactive tutorial |
| 42 | +- [Plot Types](examples/plot-types.ipynb) - All available plot types |
| 43 | +- [Advanced Usage](examples/advanced.ipynb) - Configuration and customization |
| 44 | +- [API Reference](api.md) - Full API documentation |
0 commit comments