Plotly Express Kwargs¶
All keyword arguments are passed directly to Plotly Express.
In [1]:
Copied!
import plotly.express as px
import xarray as xr
from xarray_plotly import config, xpx
config.notebook()
import plotly.express as px
import xarray as xr
from xarray_plotly import config, xpx
config.notebook()
In [2]:
Copied!
df = px.data.stocks().set_index("date")
df.index = df.index.astype("datetime64[ns]")
stocks = xr.DataArray(
df.values,
dims=["date", "company"],
coords={"date": df.index, "company": df.columns.tolist()},
name="price",
)
df = px.data.stocks().set_index("date")
df.index = df.index.astype("datetime64[ns]")
stocks = xr.DataArray(
df.values,
dims=["date", "company"],
coords={"date": df.index, "company": df.columns.tolist()},
name="price",
)
title¶
In [3]:
Copied!
xpx(stocks).line(title="Stock Prices")
xpx(stocks).line(title="Stock Prices")
template¶
See Plotly templates.
In [4]:
Copied!
xpx(stocks).line(template="plotly_dark")
xpx(stocks).line(template="plotly_dark")
In [5]:
Copied!
xpx(stocks).line(template="seaborn")
xpx(stocks).line(template="seaborn")
labels¶
Override axis and legend labels:
In [6]:
Copied!
xpx(stocks).line(labels={"price": "Normalized Price", "date": "Date", "company": "Ticker"})
xpx(stocks).line(labels={"price": "Normalized Price", "date": "Date", "company": "Ticker"})
color_discrete_sequence¶
In [7]:
Copied!
xpx(stocks).line(color_discrete_sequence=px.colors.qualitative.Set2)
xpx(stocks).line(color_discrete_sequence=px.colors.qualitative.Set2)
In [8]:
Copied!
xpx(stocks).line(
color_discrete_sequence=["#E63946", "#457B9D", "#2A9D8F", "#E9C46A", "#F4A261", "#264653"]
)
xpx(stocks).line(
color_discrete_sequence=["#E63946", "#457B9D", "#2A9D8F", "#E9C46A", "#F4A261", "#264653"]
)
color_continuous_scale¶
See Plotly colorscales.
In [9]:
Copied!
xpx(stocks).imshow(color_continuous_scale="Viridis")
xpx(stocks).imshow(color_continuous_scale="Viridis")
In [10]:
Copied!
# Diverging colorscale with midpoint
change = stocks - stocks.isel(date=0)
xpx(change).imshow(color_continuous_scale="RdBu_r", color_continuous_midpoint=0)
# Diverging colorscale with midpoint
change = stocks - stocks.isel(date=0)
xpx(change).imshow(color_continuous_scale="RdBu_r", color_continuous_midpoint=0)
colors (unified parameter)¶
The colors parameter provides a simpler way to set colors without remembering the exact Plotly parameter name. It automatically maps to the correct parameter based on the input type:
| Input | Maps To |
|---|---|
"Viridis" (continuous scale name) |
color_continuous_scale |
"D3" (qualitative palette name) |
color_discrete_sequence |
["red", "blue"] (list) |
color_discrete_sequence |
{"A": "red"} (dict) |
color_discrete_map |
In [11]:
Copied!
# Named qualitative palette
xpx(stocks).line(colors="D3")
# Named qualitative palette
xpx(stocks).line(colors="D3")
In [12]:
Copied!
# List of custom colors
xpx(stocks).line(colors=["#E63946", "#457B9D", "#2A9D8F", "#E9C46A", "#F4A261"])
# List of custom colors
xpx(stocks).line(colors=["#E63946", "#457B9D", "#2A9D8F", "#E9C46A", "#F4A261"])
In [13]:
Copied!
# Dict for explicit mapping
xpx(stocks).line(
colors={
"GOOG": "red",
"AAPL": "blue",
"AMZN": "green",
"FB": "purple",
"NFLX": "orange",
"MSFT": "brown",
}
)
# Dict for explicit mapping
xpx(stocks).line(
colors={
"GOOG": "red",
"AAPL": "blue",
"AMZN": "green",
"FB": "purple",
"NFLX": "orange",
"MSFT": "brown",
}
)
In [14]:
Copied!
# Continuous scale for heatmaps
xpx(stocks).imshow(colors="Plasma")
# Continuous scale for heatmaps
xpx(stocks).imshow(colors="Plasma")
markers¶
In [15]:
Copied!
xpx(stocks.isel(date=slice(0, 30))).line(markers=True)
xpx(stocks.isel(date=slice(0, 30))).line(markers=True)
barmode¶
See Plotly bar charts.
In [16]:
Copied!
xpx(stocks.isel(date=[0, 50, 100])).bar(barmode="group")
xpx(stocks.isel(date=[0, 50, 100])).bar(barmode="group")
text_auto¶
In [17]:
Copied!
xpx(stocks.isel(date=[0, 50, 100])).bar(barmode="group", text_auto=".2f")
xpx(stocks.isel(date=[0, 50, 100])).bar(barmode="group", text_auto=".2f")
range_x, range_y¶
In [18]:
Copied!
xpx(stocks).line(range_y=[0.5, 2.0])
xpx(stocks).line(range_y=[0.5, 2.0])
More¶
See the full Plotly Express API reference for all available kwargs.