Getting Started¶
This notebook introduces the basics of using xarray_plotly for interactive visualization.
Basic Usage¶
Import the xpx function for full IDE code completion:
In [1]:
Copied!
import plotly.express as px
import xarray as xr
from xarray_plotly import config, xpx
config.notebook() # Configure Plotly for notebook rendering
import plotly.express as px
import xarray as xr
from xarray_plotly import config, xpx
config.notebook() # Configure Plotly for notebook rendering
Load Sample Data¶
We'll use plotly's built-in stock price data and convert it to an xarray DataArray:
In [2]:
Copied!
# Load stock prices from plotly
df = px.data.stocks()
df = df.set_index("date")
df.index = df.index.astype("datetime64[ns]")
# Convert to xarray DataArray
stocks = xr.DataArray(
df.values,
dims=["date", "company"],
coords={"date": df.index, "company": df.columns.tolist()},
name="price",
attrs={"long_name": "Stock Price", "units": "normalized"},
)
stocks
# Load stock prices from plotly
df = px.data.stocks()
df = df.set_index("date")
df.index = df.index.astype("datetime64[ns]")
# Convert to xarray DataArray
stocks = xr.DataArray(
df.values,
dims=["date", "company"],
coords={"date": df.index, "company": df.columns.tolist()},
name="price",
attrs={"long_name": "Stock Price", "units": "normalized"},
)
stocks
Out[2]:
<xarray.DataArray 'price' (date: 105, company: 6)> Size: 5kB
array([[1. , 1. , 1. , 1. , 1. ,
1. ],
[1.01817228, 1.01194283, 1.06188061, 0.95996783, 1.05352631,
1.01598816],
[1.03200787, 1.01977147, 1.05324043, 0.97024344, 1.04985953,
1.02052385],
[1.06678278, 0.98005711, 1.14067562, 1.01685841, 1.30768132,
1.06656079],
[1.00877316, 0.91714286, 1.16337434, 1.01835693, 1.27353677,
1.04070753],
[0.94152767, 0.89377145, 1.08986768, 0.94252071, 1.18800893,
0.99988659],
[0.99325918, 0.98531425, 1.17862076, 0.94921057, 1.32634879,
1.04320215],
[1.02228215, 1.00285714, 1.22036544, 0.98094721, 1.3616362 ,
1.06656079],
[0.97885202, 1.00691433, 1.22056884, 0.94525014, 1.43363961,
1.0551083 ],
[1.05244827, 1.02845712, 1.28454854, 0.99132989, 1.57836085,
1.09468192],
...
[1.15560274, 1.46182861, 1.45747427, 1.03623221, 1.3658269 ,
1.6296632 ],
[1.18974263, 1.48651437, 1.45295083, 1.02135397, 1.38849469,
1.65506297],
[1.21106304, 1.51862863, 1.41520898, 1.04415306, 1.40497163,
1.70053291],
[1.17519936, 1.49588571, 1.42027755, 1.06406209, 1.47854661,
1.69622398],
[1.18392712, 1.52714286, 1.46508943, 1.07915436, 1.49845229,
1.71652117],
[1.21627974, 1.54691423, 1.42506139, 1.07599677, 1.46364112,
1.7207166 ],
[1.22282099, 1.57228568, 1.43266017, 1.03885467, 1.42149623,
1.75223943],
[1.22441776, 1.59680001, 1.45345524, 1.10409417, 1.60436205,
1.78489625],
[1.22650449, 1.65599993, 1.52122624, 1.11372759, 1.56716981,
1.80247197],
[1.21301366, 1.67799997, 1.50336003, 1.09847468, 1.5408829 ,
1.78818453]])
Coordinates:
* date (date) datetime64[ns] 840B 2018-01-01 2018-01-08 ... 2019-12-30
* company (company) <U4 96B 'GOOG' 'AAPL' 'AMZN' 'FB' 'NFLX' 'MSFT'
Attributes:
long_name: Stock Price
units: normalizedYour First Plot¶
Create an interactive line plot with a single method call:
In [3]:
Copied!
# Dimensions auto-assign: date->x, company->color
fig = xpx(stocks).line()
fig
# Dimensions auto-assign: date->x, company->color
fig = xpx(stocks).line()
fig
The plot is fully interactive:
- Zoom: Click and drag to select a region
- Pan: Hold shift and drag
- Hover: See exact values at each point
- Toggle traces: Click legend items to show/hide
Dimension Assignment¶
Dimensions are assigned to plot "slots" based on their order:
| Dimension | Slot |
|---|---|
| date (1st) | x-axis |
| company (2nd) | color |
You can override this with explicit assignments:
In [4]:
Copied!
# Put company on x-axis, date on color (just first few dates)
fig = xpx(stocks.isel(date=[0, 25, 50, 75, 100])).bar(x="company", color="date")
fig
# Put company on x-axis, date on color (just first few dates)
fig = xpx(stocks.isel(date=[0, 25, 50, 75, 100])).bar(x="company", color="date")
fig
Skipping Slots¶
Use None to skip a slot entirely:
In [5]:
Copied!
# Skip color, so company goes to line_dash instead
fig = xpx(stocks.sel(company=["GOOG", "AAPL", "MSFT"])).line(color=None)
fig
# Skip color, so company goes to line_dash instead
fig = xpx(stocks.sel(company=["GOOG", "AAPL", "MSFT"])).line(color=None)
fig
Customization¶
All methods return a Plotly Figure that you can customize:
In [6]:
Copied!
fig = xpx(stocks).line()
fig.update_layout(
title="Tech Stock Performance (2018-2019)",
template="plotly_white",
legend_title_text="Company",
)
fig
fig = xpx(stocks).line()
fig.update_layout(
title="Tech Stock Performance (2018-2019)",
template="plotly_white",
legend_title_text="Company",
)
fig
You can also pass Plotly Express arguments directly:
In [7]:
Copied!
fig = xpx(stocks).line(
title="Stock Prices",
color_discrete_sequence=["#E63946", "#457B9D", "#2A9D8F", "#E9C46A", "#F4A261", "#264653"],
template="simple_white",
)
fig
fig = xpx(stocks).line(
title="Stock Prices",
color_discrete_sequence=["#E63946", "#457B9D", "#2A9D8F", "#E9C46A", "#F4A261", "#264653"],
template="simple_white",
)
fig
Configuration¶
Customize label extraction and other behavior with the config module:
In [8]:
Copied!
# View current options
config.get_options()
# View current options
config.get_options()
Out[8]:
{'label_use_long_name': True,
'label_use_standard_name': True,
'label_include_units': True,
'label_unit_format': '[{units}]',
'slot_orders': {'line': ('x',
'color',
'line_dash',
'symbol',
'facet_col',
'facet_row',
'animation_frame'),
'bar': ('x',
'color',
'pattern_shape',
'facet_col',
'facet_row',
'animation_frame'),
'fast_bar': ('x', 'color', 'facet_col', 'facet_row', 'animation_frame'),
'area': ('x',
'color',
'pattern_shape',
'facet_col',
'facet_row',
'animation_frame'),
'scatter': ('x',
'color',
'symbol',
'facet_col',
'facet_row',
'animation_frame'),
'imshow': ('y', 'x', 'facet_col', 'animation_frame'),
'box': ('x', 'color', 'facet_col', 'facet_row', 'animation_frame'),
'pie': ('names', 'facet_col', 'facet_row')},
'dataset_variable_position': 1}
Next Steps¶
- Explore different Plot Types
- Learn Advanced Usage patterns
- Check the API Reference