API Reference¶
xarray_plotly.xpx(data)
¶
Get the plotly accessor for a DataArray or Dataset with full IDE code completion.
This is an alternative to da.plotly / ds.plotly that provides proper type hints
and code completion in IDEs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray | Dataset
|
The DataArray or Dataset to plot. |
required |
Returns:
| Type | Description |
|---|---|
DataArrayPlotlyAccessor | DatasetPlotlyAccessor
|
The accessor with plotting methods (line, bar, area, scatter, box, imshow). |
Example
Source code in xarray_plotly/__init__.py
xarray_plotly.accessor.DataArrayPlotlyAccessor
¶
Plotly Express plotting accessor for xarray DataArray.
Dimensions are automatically assigned to plot slots by position. All methods return Plotly Figure objects for interactive visualization.
Available methods: line, bar, area, scatter, box, imshow
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
darray
|
DataArray
|
The DataArray to plot. |
required |
Example
import xarray as xr
import numpy as np
da = xr.DataArray(np.random.rand(10, 3), dims=["time", "city"])
fig = da.plotly.line() # Auto: time->x, city->color
fig = da.plotly.line(color="time", x="city") # Explicit
fig = da.plotly.line(color=None) # Skip slot
fig.update_layout(title="My Plot") # Customize
Source code in xarray_plotly/accessor.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
line(*, x=auto, color=auto, line_dash=auto, symbol=auto, facet_col=auto, facet_row=auto, animation_frame=auto, colors=None, **px_kwargs)
¶
Create an interactive line plot.
Slot order: x -> color -> line_dash -> symbol -> facet_col -> facet_row -> animation_frame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SlotValue
|
Dimension for x-axis. Default: first dimension. |
auto
|
color
|
SlotValue
|
Dimension for color grouping. Default: second dimension. |
auto
|
line_dash
|
SlotValue
|
Dimension for line dash style. Default: third dimension. |
auto
|
symbol
|
SlotValue
|
Dimension for marker symbol. Default: fourth dimension. |
auto
|
facet_col
|
SlotValue
|
Dimension for subplot columns. Default: fifth dimension. |
auto
|
facet_row
|
SlotValue
|
Dimension for subplot rows. Default: sixth dimension. |
auto
|
animation_frame
|
SlotValue
|
Dimension for animation. Default: seventh dimension. |
auto
|
colors
|
Colors
|
Color specification (scale name, list, or dict). See module docs. |
None
|
**px_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Interactive Plotly Figure. |
Source code in xarray_plotly/accessor.py
bar(*, x=auto, color=auto, pattern_shape=auto, facet_col=auto, facet_row=auto, animation_frame=auto, colors=None, **px_kwargs)
¶
Create an interactive bar chart.
Slot order: x -> color -> pattern_shape -> facet_col -> facet_row -> animation_frame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SlotValue
|
Dimension for x-axis. Default: first dimension. |
auto
|
color
|
SlotValue
|
Dimension for color grouping. Default: second dimension. |
auto
|
pattern_shape
|
SlotValue
|
Dimension for bar fill pattern. Default: third dimension. |
auto
|
facet_col
|
SlotValue
|
Dimension for subplot columns. Default: fourth dimension. |
auto
|
facet_row
|
SlotValue
|
Dimension for subplot rows. Default: fifth dimension. |
auto
|
animation_frame
|
SlotValue
|
Dimension for animation. Default: sixth dimension. |
auto
|
colors
|
Colors
|
Color specification (scale name, list, or dict). See module docs. |
None
|
**px_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Interactive Plotly Figure. |
Source code in xarray_plotly/accessor.py
area(*, x=auto, color=auto, pattern_shape=auto, facet_col=auto, facet_row=auto, animation_frame=auto, colors=None, **px_kwargs)
¶
Create an interactive stacked area chart.
Slot order: x -> color -> pattern_shape -> facet_col -> facet_row -> animation_frame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SlotValue
|
Dimension for x-axis. Default: first dimension. |
auto
|
color
|
SlotValue
|
Dimension for color/stacking. Default: second dimension. |
auto
|
pattern_shape
|
SlotValue
|
Dimension for fill pattern. Default: third dimension. |
auto
|
facet_col
|
SlotValue
|
Dimension for subplot columns. Default: fourth dimension. |
auto
|
facet_row
|
SlotValue
|
Dimension for subplot rows. Default: fifth dimension. |
auto
|
animation_frame
|
SlotValue
|
Dimension for animation. Default: sixth dimension. |
auto
|
colors
|
Colors
|
Color specification (scale name, list, or dict). See module docs. |
None
|
**px_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Interactive Plotly Figure. |
Source code in xarray_plotly/accessor.py
scatter(*, x=auto, y='value', color=auto, symbol=auto, facet_col=auto, facet_row=auto, animation_frame=auto, colors=None, **px_kwargs)
¶
Create an interactive scatter plot.
By default, y-axis shows the DataArray values. Set y to a dimension name to create dimension-vs-dimension plots (e.g., lat vs lon).
Slot order: x -> color -> symbol -> facet_col -> facet_row -> animation_frame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SlotValue
|
Dimension for x-axis. Default: first dimension. |
auto
|
y
|
SlotValue | str
|
What to plot on y-axis. Default "value" uses DataArray values. Can be a dimension name for dimension vs dimension plots. |
'value'
|
color
|
SlotValue
|
Dimension for color grouping, or "value" for DataArray values. |
auto
|
symbol
|
SlotValue
|
Dimension for marker symbol. Default: third dimension. |
auto
|
facet_col
|
SlotValue
|
Dimension for subplot columns. Default: fourth dimension. |
auto
|
facet_row
|
SlotValue
|
Dimension for subplot rows. Default: fifth dimension. |
auto
|
animation_frame
|
SlotValue
|
Dimension for animation. Default: sixth dimension. |
auto
|
colors
|
Colors
|
Color specification (scale name, list, or dict). See module docs. |
None
|
**px_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Interactive Plotly Figure. |
Source code in xarray_plotly/accessor.py
box(*, x=auto, color=None, facet_col=None, facet_row=None, animation_frame=None, colors=None, **px_kwargs)
¶
Create an interactive box plot.
By default, only the first dimension is assigned to x; all other dimensions are aggregated into the box statistics.
Slot order: x -> color -> facet_col -> facet_row -> animation_frame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SlotValue
|
Dimension for x-axis categories. Default: first dimension. |
auto
|
color
|
SlotValue
|
Dimension for color grouping. Default: None (aggregated). |
None
|
facet_col
|
SlotValue
|
Dimension for subplot columns. Default: None (aggregated). |
None
|
facet_row
|
SlotValue
|
Dimension for subplot rows. Default: None (aggregated). |
None
|
animation_frame
|
SlotValue
|
Dimension for animation. Default: None (aggregated). |
None
|
colors
|
Colors
|
Color specification (scale name, list, or dict). See module docs. |
None
|
**px_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Interactive Plotly Figure. |
Source code in xarray_plotly/accessor.py
imshow(*, x=auto, y=auto, facet_col=auto, animation_frame=auto, robust=False, colors=None, **px_kwargs)
¶
Create an interactive heatmap image.
Slot order: y (rows) -> x (columns) -> facet_col -> animation_frame
Note
Difference from px.imshow: Color bounds are computed from the
entire dataset by default, ensuring consistent coloring across
animation frames. Use zmin/zmax to override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SlotValue
|
Dimension for x-axis (columns). Default: second dimension. |
auto
|
y
|
SlotValue
|
Dimension for y-axis (rows). Default: first dimension. |
auto
|
facet_col
|
SlotValue
|
Dimension for subplot columns. Default: third dimension. |
auto
|
animation_frame
|
SlotValue
|
Dimension for animation. Default: fourth dimension. |
auto
|
robust
|
bool
|
If True, use 2nd/98th percentiles for color bounds (handles outliers). |
False
|
colors
|
Colors
|
Color scale name (e.g., "Viridis", "RdBu"). See module docs. |
None
|
**px_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
Interactive Plotly Figure. |
Source code in xarray_plotly/accessor.py
xarray_plotly.config
¶
Configuration for xarray_plotly.
This module provides a global configuration system similar to xarray and pandas, allowing users to customize label extraction and slot assignment behavior.
Options
dataclass
¶
Configuration options for xarray_plotly.
Attributes:
| Name | Type | Description |
|---|---|---|
label_use_long_name |
bool
|
Use |
label_use_standard_name |
bool
|
Fall back to |
label_include_units |
bool
|
Append units to labels. Default True. |
label_unit_format |
str
|
Format string for units. Use |
slot_orders |
dict[str, tuple[str, ...]]
|
Slot orders per plot type. Keys are plot types, values are tuples. |
dataset_variable_position |
int
|
Position of "variable" dim when plotting all Dataset variables. Default 1 (second position, typically color). Set to 0 for first position (x-axis), or -1 for last position. |
Source code in xarray_plotly/config.py
to_dict()
¶
Return options as a dictionary.
Source code in xarray_plotly/config.py
notebook(renderer='notebook')
¶
Configure Plotly for Jupyter notebook rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
renderer
|
str
|
The Plotly renderer to use. Default is "notebook". Other options include "jupyterlab", "colab", "kaggle", etc. |
'notebook'
|
Source code in xarray_plotly/config.py
get_options()
¶
Get the current xarray_plotly options.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary of current option values. |
Source code in xarray_plotly/config.py
set_options(*, label_use_long_name=None, label_use_standard_name=None, label_include_units=None, label_unit_format=None, slot_orders=None, dataset_variable_position=None)
¶
Set xarray_plotly options globally or as a context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_use_long_name
|
bool | None
|
Use |
None
|
label_use_standard_name
|
bool | None
|
Fall back to |
None
|
label_include_units
|
bool | None
|
Append units to labels. |
None
|
label_unit_format
|
str | None
|
Format string for units. Use |
None
|
slot_orders
|
dict[str, tuple[str, ...]] | None
|
Slot orders per plot type. |
None
|
dataset_variable_position
|
int | None
|
Position of "variable" dim when plotting all Dataset variables. Default 1 (second, typically color). Use 0 for first, -1 for last. |
None
|
Yields:
| Type | Description |
|---|---|
None
|
None when used as a context manager. |
Example
Source code in xarray_plotly/config.py
xarray_plotly.common
¶
Common utilities for dimension-to-slot assignment and data conversion.
auto = _AUTO()
module-attribute
¶
SLOT_ORDERS = DEFAULT_SLOT_ORDERS
module-attribute
¶
Slot orders per plot type.
For most plots, y-axis shows DataArray values (not a dimension slot). For imshow, both y and x are dimensions (rows and columns of the heatmap).
Note
To customize slot orders, use config.set_options(slot_orders=...).
assign_slots(dims, plot_type, *, allow_unassigned=False, **slot_kwargs)
¶
Assign dimensions to plot slots based on position.
Positional assignment: dimensions fill slots in order. - Explicit assignments lock a dimension to a slot - None skips a slot - Remaining dims fill remaining slots by position - Error if dims left over after all slots filled (unless allow_unassigned=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dims
|
Sequence[Hashable]
|
Dimension names from the DataArray. |
required |
plot_type
|
str
|
Type of plot (line, bar, area, scatter, box, imshow). |
required |
allow_unassigned
|
bool
|
If True, allow dimensions to remain unassigned. |
False
|
**slot_kwargs
|
SlotValue
|
Explicit slot assignments. Use |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Hashable]
|
Mapping of slot names to dimension names. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If plot_type is unknown, dimension doesn't exist, or dimensions are left unassigned (unless allow_unassigned=True). |
Example
assign_slots(["time", "city", "scenario"], "line")
# {'x': 'time', 'color': 'city', 'line_dash': 'scenario'}
assign_slots(["time", "city"], "line", color="time", x="city")
# {'x': 'city', 'color': 'time'}
assign_slots(["time", "city", "scenario"], "line", color=None)
# {'x': 'time', 'line_dash': 'city', 'symbol': 'scenario'}
Source code in xarray_plotly/common.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |