|
1 | 1 | # xarray_plotly |
2 | 2 |
|
3 | | -**Interactive Plotly Express plotting accessor for xarray** |
| 3 | +**Interactive Plotly Express plotting for xarray** |
4 | 4 |
|
5 | 5 | [](https://badge.fury.io/py/xarray_plotly) |
6 | 6 | [](https://pypi.org/project/xarray_plotly/) |
7 | | - |
8 | | -xarray_plotly provides a `plotly` accessor for xarray DataArray objects that enables interactive plotting using Plotly Express with automatic dimension-to-slot assignment. |
| 7 | +[](https://fbumann.github.io/xarray_plotly/) |
9 | 8 |
|
10 | 9 | ## Installation |
11 | 10 |
|
12 | 11 | ```bash |
13 | 12 | pip install xarray_plotly |
14 | 13 | ``` |
15 | 14 |
|
16 | | -Or with uv: |
17 | | - |
18 | | -```bash |
19 | | -uv add xarray_plotly |
20 | | -``` |
21 | | - |
22 | 15 | ## Quick Start |
23 | 16 |
|
24 | 17 | ```python |
25 | 18 | import xarray as xr |
26 | 19 | import numpy as np |
27 | | -from xarray_plotly import xpx |
| 20 | +import xarray_plotly # registers the accessor |
28 | 21 |
|
29 | | -# Create sample data |
30 | 22 | da = xr.DataArray( |
31 | | - np.random.randn(100, 3, 2).cumsum(axis=0), |
32 | | - dims=["time", "city", "scenario"], |
33 | | - coords={ |
34 | | - "time": np.arange(100), |
35 | | - "city": ["NYC", "LA", "Chicago"], |
36 | | - "scenario": ["baseline", "warming"], |
37 | | - }, |
38 | | - name="temperature", |
| 23 | + np.random.randn(100, 3).cumsum(axis=0), |
| 24 | + dims=["time", "city"], |
| 25 | + coords={"time": np.arange(100), "city": ["NYC", "LA", "Chicago"]}, |
39 | 26 | ) |
40 | 27 |
|
41 | | -# Create an interactive line plot |
42 | | -# Dimensions auto-assign: time→x, city→color, scenario→facet_col |
43 | | -fig = xpx(da).line() |
| 28 | +# Accessor style |
| 29 | +fig = da.plotly.line() |
44 | 30 | fig.show() |
45 | 31 |
|
46 | | -# Easy customization |
47 | | -fig.update_layout(title="Temperature Projections", template="plotly_dark") |
48 | | -``` |
49 | | - |
50 | | -## Usage Styles |
51 | | - |
52 | | -xarray_plotly supports two equivalent usage styles: |
53 | | - |
54 | | -```python |
55 | | -# Function style (recommended) - full IDE code completion |
| 32 | +# Or with xpx() for IDE code completion |
56 | 33 | from xarray_plotly import xpx |
57 | 34 | fig = xpx(da).line() |
58 | | - |
59 | | -# Accessor style - works but no IDE completion |
60 | | -import xarray_plotly |
61 | | -fig = da.plotly.line() |
62 | 35 | ``` |
63 | 36 |
|
64 | | -The `xpx()` function is recommended as it provides full IDE code completion and type hints. |
65 | | - |
66 | | -**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. |
67 | | - |
68 | | -## Features |
69 | | - |
70 | | -- **Interactive plots**: Zoom, pan, hover for values, toggle traces |
71 | | -- **Automatic dimension assignment**: Dimensions fill plot slots by position |
72 | | -- **Easy customization**: Returns Plotly `Figure` objects |
73 | | -- **Multiple plot types**: `line()`, `bar()`, `area()`, `scatter()`, `box()`, `imshow()` |
74 | | -- **Faceting and animation**: Built-in support for subplot grids and animations |
75 | | - |
76 | | -## Dimension Assignment |
77 | | - |
78 | | -Dimensions are automatically assigned to plot "slots" based on their order: |
79 | | - |
80 | | -```python |
81 | | -# dims: (time, city, scenario) |
82 | | -# auto-assigns: time→x, city→color, scenario→facet_col |
83 | | -xpx(da).line() |
84 | | - |
85 | | -# Override with explicit assignments |
86 | | -xpx(da).line(x="time", color="scenario", facet_col="city") |
87 | | - |
88 | | -# Skip a slot with None |
89 | | -xpx(da).line(color=None) # time→x, city→facet_col |
90 | | -``` |
91 | | - |
92 | | -## Available Methods |
93 | | - |
94 | | -| Method | Description | Slot Order | |
95 | | -|--------|-------------|------------| |
96 | | -| `line()` | Line plot | x → color → line_dash → symbol → facet_col → facet_row → animation_frame | |
97 | | -| `bar()` | Bar chart | x → color → pattern_shape → facet_col → facet_row → animation_frame | |
98 | | -| `area()` | Stacked area | x → color → pattern_shape → facet_col → facet_row → animation_frame | |
99 | | -| `scatter()` | Scatter plot | x → color → symbol → facet_col → facet_row → animation_frame | |
100 | | -| `box()` | Box plot | x → color → facet_col → facet_row → animation_frame | |
101 | | -| `imshow()` | Heatmap | y → x → facet_col → animation_frame | |
102 | | - |
103 | | -## Configuration |
104 | | - |
105 | | -Customize label extraction and slot assignment behavior: |
106 | | - |
107 | | -```python |
108 | | -from xarray_plotly import config, xpx |
109 | | - |
110 | | -# View current options |
111 | | -config.get_options() |
112 | | - |
113 | | -# Set globally (temporary) |
114 | | -with config.set_options(label_include_units=False): |
115 | | - fig = xpx(da).line() # Labels won't include units |
116 | | -``` |
117 | | - |
118 | | -**Available options:** |
119 | | - |
120 | | -| Option | Default | Description | |
121 | | -|--------|---------|-------------| |
122 | | -| `label_use_long_name` | `True` | Use `long_name` attr for labels | |
123 | | -| `label_use_standard_name` | `True` | Fall back to `standard_name` | |
124 | | -| `label_include_units` | `True` | Append units to labels | |
125 | | -| `label_unit_format` | `"[{units}]"` | Format string for units | |
126 | | -| `slot_orders` | (defaults) | Slot orders per plot type | |
127 | | - |
128 | 37 | ## Documentation |
129 | 38 |
|
130 | | -Full documentation with examples: [https://fbumann.github.io/xarray_plotly](https://fbumann.github.io/xarray_plotly) |
| 39 | +Full documentation: [https://fbumann.github.io/xarray_plotly](https://fbumann.github.io/xarray_plotly) |
131 | 40 |
|
132 | 41 | ## License |
133 | 42 |
|
|
0 commit comments