|
12 | 12 | - **Customizable**: Returns Plotly Figure objects for further modification |
13 | 13 |
|
14 | 14 | Usage: |
15 | | -Mos Recommended:: |
16 | | -
|
17 | | - import xarray_plotly as xpx |
18 | | -
|
19 | | - fig = xpx(da).line() # Create plots |
20 | | - combined = xpx.overlay(fig1, fig2) # Use helper functions |
21 | | -
|
22 | 15 | Accessor style:: |
23 | 16 |
|
24 | 17 | import xarray_plotly |
25 | 18 | fig = da.plotly.line() |
| 19 | + fig = ds.plotly.line() # Dataset: all variables |
| 20 | +
|
| 21 | + Function style (recommended for IDE completion):: |
| 22 | +
|
| 23 | + from xarray_plotly import xpx |
| 24 | + fig = xpx(da).line() |
| 25 | + fig = xpx(ds).line() # Dataset: all variables |
26 | 26 |
|
27 | 27 | Example: |
28 | 28 | ```python |
29 | 29 | import xarray as xr |
30 | 30 | import numpy as np |
31 | | - import xarray_plotly as xpx |
| 31 | + from xarray_plotly import xpx |
32 | 32 |
|
33 | 33 | da = xr.DataArray( |
34 | 34 | np.random.rand(10, 3, 2), |
35 | 35 | dims=["time", "city", "scenario"], |
36 | 36 | ) |
37 | 37 | fig = xpx(da).line() # Auto: time->x, city->color, scenario->facet_col |
| 38 | + fig = xpx(da).line(x="time", color="scenario") # Explicit |
| 39 | + fig = xpx(da).line(color=None) # Skip slot |
38 | 40 |
|
39 | | - # Combine figures |
40 | | - area = xpx(da).area() |
41 | | - line = xpx(da).line() |
42 | | - combined = xpx.overlay(area, line) |
| 41 | + # Dataset: plot all variables (accessor or xpx) |
| 42 | + ds = xr.Dataset({"temp": da, "precip": da}) |
| 43 | + fig = xpx(ds).line() # "variable" dimension for color |
| 44 | + fig = xpx(ds).line(facet_col="variable") # Facet by variable |
43 | 45 | ``` |
44 | 46 | """ |
45 | 47 |
|
46 | | -from __future__ import annotations |
47 | | - |
48 | | -import sys |
49 | | -import types |
50 | 48 | from importlib.metadata import version |
51 | | -from typing import TYPE_CHECKING, overload |
| 49 | +from typing import overload |
52 | 50 |
|
53 | 51 | from xarray import DataArray, Dataset, register_dataarray_accessor, register_dataset_accessor |
54 | 52 |
|
|
68 | 66 | "config", |
69 | 67 | "overlay", |
70 | 68 | "update_traces", |
| 69 | + "xpx", |
71 | 70 | ] |
72 | 71 |
|
73 | | -__version__ = version("xarray_plotly") |
74 | | - |
75 | | -# Register the accessors |
76 | | -register_dataarray_accessor("plotly")(DataArrayPlotlyAccessor) |
77 | | -register_dataset_accessor("plotly")(DatasetPlotlyAccessor) |
78 | | - |
79 | | - |
80 | | -class _CallableModule(types.ModuleType): |
81 | | - """A module that can be called as a function. |
82 | 72 |
|
83 | | - Enables the pattern:: |
84 | | -
|
85 | | - import xarray_plotly as xpx |
86 | | - fig = xpx(da).line() # Call module as function |
87 | | - fig = xpx.overlay(a, b) # Access module attributes |
88 | | - """ |
| 73 | +@overload |
| 74 | +def xpx(data: DataArray) -> DataArrayPlotlyAccessor: ... |
89 | 75 |
|
90 | | - @overload |
91 | | - def __call__(self, data: DataArray) -> DataArrayPlotlyAccessor: ... |
92 | 76 |
|
93 | | - @overload |
94 | | - def __call__(self, data: Dataset) -> DatasetPlotlyAccessor: ... |
| 77 | +@overload |
| 78 | +def xpx(data: Dataset) -> DatasetPlotlyAccessor: ... |
95 | 79 |
|
96 | | - def __call__( |
97 | | - self, data: DataArray | Dataset |
98 | | - ) -> DataArrayPlotlyAccessor | DatasetPlotlyAccessor: |
99 | | - """Get the plotly accessor for a DataArray or Dataset. |
100 | 80 |
|
101 | | - Args: |
102 | | - data: The DataArray or Dataset to plot. |
| 81 | +def xpx(data: DataArray | Dataset) -> DataArrayPlotlyAccessor | DatasetPlotlyAccessor: |
| 82 | + """Get the plotly accessor for a DataArray or Dataset with full IDE code completion. |
103 | 83 |
|
104 | | - Returns: |
105 | | - The accessor with plotting methods (line, bar, area, scatter, box, imshow, pie). |
| 84 | + This is an alternative to `da.plotly` / `ds.plotly` that provides proper type hints |
| 85 | + and code completion in IDEs. |
106 | 86 |
|
107 | | - Example: |
108 | | - ```python |
109 | | - import xarray_plotly as xpx |
| 87 | + Args: |
| 88 | + data: The DataArray or Dataset to plot. |
110 | 89 |
|
111 | | - fig = xpx(da).line() |
112 | | - fig = xpx(ds).line(var="temperature") |
113 | | - ``` |
114 | | - """ |
115 | | - if isinstance(data, Dataset): |
116 | | - return DatasetPlotlyAccessor(data) |
117 | | - return DataArrayPlotlyAccessor(data) |
| 90 | + Returns: |
| 91 | + The accessor with plotting methods (line, bar, area, scatter, box, imshow). |
118 | 92 |
|
| 93 | + Example: |
| 94 | + ```python |
| 95 | + from xarray_plotly import xpx |
119 | 96 |
|
120 | | -# Make the module callable |
121 | | -sys.modules[__name__].__class__ = _CallableModule |
| 97 | + # DataArray |
| 98 | + fig = xpx(da).line() # Full code completion works here |
122 | 99 |
|
123 | | -# For type checking, expose the call signature |
124 | | -if TYPE_CHECKING: |
| 100 | + # Dataset |
| 101 | + fig = xpx(ds).line() # Plots all variables |
| 102 | + fig = xpx(ds).line(var="temperature") # Single variable |
| 103 | + ``` |
| 104 | + """ |
| 105 | + if isinstance(data, Dataset): |
| 106 | + return DatasetPlotlyAccessor(data) |
| 107 | + return DataArrayPlotlyAccessor(data) |
125 | 108 |
|
126 | | - @overload |
127 | | - def __call__(data: DataArray) -> DataArrayPlotlyAccessor: ... |
128 | 109 |
|
129 | | - @overload |
130 | | - def __call__(data: Dataset) -> DatasetPlotlyAccessor: ... |
| 110 | +__version__ = version("xarray_plotly") |
131 | 111 |
|
132 | | - def __call__( |
133 | | - data: DataArray | Dataset, |
134 | | - ) -> DataArrayPlotlyAccessor | DatasetPlotlyAccessor: ... |
| 112 | +# Register the accessors |
| 113 | +register_dataarray_accessor("plotly")(DataArrayPlotlyAccessor) |
| 114 | +register_dataset_accessor("plotly")(DatasetPlotlyAccessor) |
0 commit comments