Skip to content

Commit e49ec17

Browse files
authored
Add pie plots (#8)
1 parent c04a25b commit e49ec17

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

xarray_plotly/accessor.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DataArrayPlotlyAccessor:
3333
```
3434
"""
3535

36-
__all__: ClassVar = ["line", "bar", "area", "scatter", "box", "imshow"]
36+
__all__: ClassVar = ["line", "bar", "area", "scatter", "box", "imshow", "pie"]
3737

3838
def __init__(self, darray: DataArray) -> None:
3939
self._da = darray
@@ -274,6 +274,38 @@ def imshow(
274274
**px_kwargs,
275275
)
276276

277+
def pie(
278+
self,
279+
*,
280+
names: SlotValue = auto,
281+
color: SlotValue = None,
282+
facet_col: SlotValue = auto,
283+
facet_row: SlotValue = auto,
284+
**px_kwargs: Any,
285+
) -> go.Figure:
286+
"""Create an interactive pie chart.
287+
288+
Slot order: names -> facet_col -> facet_row
289+
290+
Args:
291+
names: Dimension for pie slice names/categories. Default: first dimension.
292+
color: Dimension for color grouping. Default: None (uses names).
293+
facet_col: Dimension for subplot columns. Default: second dimension.
294+
facet_row: Dimension for subplot rows. Default: third dimension.
295+
**px_kwargs: Additional arguments passed to `plotly.express.pie()`.
296+
297+
Returns:
298+
Interactive Plotly Figure.
299+
"""
300+
return plotting.pie(
301+
self._da,
302+
names=names,
303+
color=color,
304+
facet_col=facet_col,
305+
facet_row=facet_row,
306+
**px_kwargs,
307+
)
308+
277309

278310
class DatasetPlotlyAccessor:
279311
"""Plotly Express plotting accessor for xarray Dataset.
@@ -307,7 +339,7 @@ class DatasetPlotlyAccessor:
307339
```
308340
"""
309341

310-
__all__: ClassVar = ["line", "bar", "area", "scatter", "box"]
342+
__all__: ClassVar = ["line", "bar", "area", "scatter", "box", "pie"]
311343

312344
def __init__(self, dataset: Dataset) -> None:
313345
self._ds = dataset
@@ -519,3 +551,36 @@ def box(
519551
animation_frame=animation_frame,
520552
**px_kwargs,
521553
)
554+
555+
def pie(
556+
self,
557+
var: str | None = None,
558+
*,
559+
names: SlotValue = auto,
560+
color: SlotValue = None,
561+
facet_col: SlotValue = auto,
562+
facet_row: SlotValue = auto,
563+
**px_kwargs: Any,
564+
) -> go.Figure:
565+
"""Create an interactive pie chart.
566+
567+
Args:
568+
var: Variable to plot. If None, plots all variables with "variable" dimension.
569+
names: Dimension for pie slice names/categories.
570+
color: Dimension for color grouping.
571+
facet_col: Dimension for subplot columns.
572+
facet_row: Dimension for subplot rows.
573+
**px_kwargs: Additional arguments passed to `plotly.express.pie()`.
574+
575+
Returns:
576+
Interactive Plotly Figure.
577+
"""
578+
da = self._get_dataarray(var)
579+
return plotting.pie(
580+
da,
581+
names=names,
582+
color=color,
583+
facet_col=facet_col,
584+
facet_row=facet_row,
585+
**px_kwargs,
586+
)

xarray_plotly/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
),
4545
"imshow": ("y", "x", "facet_col", "animation_frame"),
4646
"box": ("x", "color", "facet_col", "facet_row", "animation_frame"),
47+
"pie": ("names", "facet_col", "facet_row"),
4748
}
4849

4950

xarray_plotly/plotting.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,64 @@ def imshow(
446446
animation_frame=slots.get("animation_frame"),
447447
**px_kwargs,
448448
)
449+
450+
451+
def pie(
452+
darray: DataArray,
453+
*,
454+
names: SlotValue = auto,
455+
color: SlotValue = None,
456+
facet_col: SlotValue = auto,
457+
facet_row: SlotValue = auto,
458+
**px_kwargs: Any,
459+
) -> go.Figure:
460+
"""
461+
Create an interactive pie chart from a DataArray.
462+
463+
The values are the DataArray values. Dimensions fill slots in order:
464+
names -> facet_col -> facet_row
465+
466+
Parameters
467+
----------
468+
darray
469+
The DataArray to plot.
470+
names
471+
Dimension for pie slice names/categories. Default: first dimension.
472+
color
473+
Dimension for color grouping. Default: None (uses names).
474+
facet_col
475+
Dimension for subplot columns. Default: second dimension.
476+
facet_row
477+
Dimension for subplot rows. Default: third dimension.
478+
**px_kwargs
479+
Additional arguments passed to `plotly.express.pie()`.
480+
481+
Returns
482+
-------
483+
plotly.graph_objects.Figure
484+
"""
485+
slots = assign_slots(
486+
list(darray.dims),
487+
"pie",
488+
names=names,
489+
facet_col=facet_col,
490+
facet_row=facet_row,
491+
)
492+
493+
df = to_dataframe(darray)
494+
value_col = get_value_col(darray)
495+
labels = {**build_labels(darray, slots, value_col), **px_kwargs.pop("labels", {})}
496+
497+
# Use names dimension for color if not explicitly set
498+
color_col = color if color is not None else slots.get("names")
499+
500+
return px.pie(
501+
df,
502+
names=slots.get("names"),
503+
values=value_col,
504+
color=color_col,
505+
facet_col=slots.get("facet_col"),
506+
facet_row=slots.get("facet_row"),
507+
labels=labels,
508+
**px_kwargs,
509+
)

0 commit comments

Comments
 (0)