Dataset Plotting¶
Plot multiple variables from an xarray Dataset with automatic or custom slot assignment.
In [1]:
Copied!
import numpy as np
import xarray as xr
from xarray_plotly import config, xpx
config.notebook()
import numpy as np
import xarray as xr
from xarray_plotly import config, xpx
config.notebook()
In [2]:
Copied!
# Create a Dataset with multiple variables
time = np.arange(50)
cities = ["NYC", "LA", "Chicago"]
ds = xr.Dataset(
{
"temperature": (["time", "city"], 20 + 5 * np.random.randn(50, 3).cumsum(axis=0) / 10),
"humidity": (["time", "city"], 50 + 10 * np.random.randn(50, 3).cumsum(axis=0) / 10),
"pressure": (["time", "city"], 1013 + np.random.randn(50, 3).cumsum(axis=0)),
},
coords={"time": time, "city": cities},
)
ds
# Create a Dataset with multiple variables
time = np.arange(50)
cities = ["NYC", "LA", "Chicago"]
ds = xr.Dataset(
{
"temperature": (["time", "city"], 20 + 5 * np.random.randn(50, 3).cumsum(axis=0) / 10),
"humidity": (["time", "city"], 50 + 10 * np.random.randn(50, 3).cumsum(axis=0) / 10),
"pressure": (["time", "city"], 1013 + np.random.randn(50, 3).cumsum(axis=0)),
},
coords={"time": time, "city": cities},
)
ds
Out[2]:
<xarray.Dataset> Size: 4kB
Dimensions: (time: 50, city: 3)
Coordinates:
* time (time) int64 400B 0 1 2 3 4 5 6 7 8 ... 42 43 44 45 46 47 48 49
* city (city) <U7 84B 'NYC' 'LA' 'Chicago'
Data variables:
temperature (time, city) float64 1kB 20.32 19.74 20.48 ... 19.11 26.35
humidity (time, city) float64 1kB 50.65 49.89 51.17 ... 42.46 63.34 67.0
pressure (time, city) float64 1kB 1.012e+03 1.014e+03 ... 1.007e+03Plot All Variables¶
When you call a plot method on a Dataset without specifying var, all variables are combined into a single DataArray with a new "variable" dimension:
In [3]:
Copied!
# All variables: time -> x, variable -> color, city -> line_dash
xpx(ds).line()
# All variables: time -> x, variable -> color, city -> line_dash
xpx(ds).line()
Control Where "variable" Goes¶
The "variable" dimension can be assigned to any slot:
In [4]:
Copied!
# Variables as facet columns
xpx(ds).line(facet_col="variable")
# Variables as facet columns
xpx(ds).line(facet_col="variable")
In [5]:
Copied!
# Variables as rows, cities as columns
xpx(ds).line(facet_row="variable", facet_col="city")
# Variables as rows, cities as columns
xpx(ds).line(facet_row="variable", facet_col="city")
Configure Default "variable" Position¶
By default, "variable" is placed as the second dimension so it maps to color. This keeps your first dimension (e.g., time) on the x-axis.
You can change this globally with config.set_options():
In [6]:
Copied!
# Default: position=1 (second) -> variable goes to color
# Note: to_array() puts "variable" first, but xpx() reorders it to position 1
print("Raw to_array() dims:", ds.to_array().dims) # (variable, time, city)
print("After xpx reorder: (time, variable, city)") # time->x, variable->color
xpx(ds).line(title="Default: variable as color (position=1)")
# Default: position=1 (second) -> variable goes to color
# Note: to_array() puts "variable" first, but xpx() reorders it to position 1
print("Raw to_array() dims:", ds.to_array().dims) # (variable, time, city)
print("After xpx reorder: (time, variable, city)") # time->x, variable->color
xpx(ds).line(title="Default: variable as color (position=1)")
Raw to_array() dims: ('variable', 'time', 'city')
After xpx reorder: (time, variable, city)
In [7]:
Copied!
# Position 0: variable goes first (x-axis) - usually not what you want!
with config.set_options(dataset_variable_position=0):
fig = xpx(ds).line(title="position=0: variable on x-axis (probably not desired)")
fig
# Position 0: variable goes first (x-axis) - usually not what you want!
with config.set_options(dataset_variable_position=0):
fig = xpx(ds).line(title="position=0: variable on x-axis (probably not desired)")
fig
In [8]:
Copied!
# Position -1: variable goes last -> city gets color, variable gets line_dash
with config.set_options(dataset_variable_position=-1):
fig = xpx(ds).line(title="position=-1: variable as line_dash")
fig
# Position -1: variable goes last -> city gets color, variable gets line_dash
with config.set_options(dataset_variable_position=-1):
fig = xpx(ds).line(title="position=-1: variable as line_dash")
fig
Plot a Single Variable¶
Use var="name" to plot just one variable:
In [9]:
Copied!
xpx(ds).line(var="temperature", title="Temperature Only")
xpx(ds).line(var="temperature", title="Temperature Only")
Different Plot Types¶
In [10]:
Copied!
# Bar chart - latest values by city
xpx(ds.isel(time=-1)).bar(x="city", color="variable", barmode="group")
# Bar chart - latest values by city
xpx(ds.isel(time=-1)).bar(x="city", color="variable", barmode="group")
In [11]:
Copied!
# Box plot - distribution by variable
xpx(ds).box(x="variable", color="city")
# Box plot - distribution by variable
xpx(ds).box(x="variable", color="city")
In [12]:
Copied!
# Area chart
xpx(ds).area(var="humidity", title="Humidity Over Time")
# Area chart
xpx(ds).area(var="humidity", title="Humidity Over Time")
In [13]:
Copied!
# Scatter
xpx(ds).scatter(var="temperature", title="Temperature Scatter")
# Scatter
xpx(ds).scatter(var="temperature", title="Temperature Scatter")
In [14]:
Copied!
# Pie chart - snapshot at one time
xpx(ds.isel(time=-1)).pie(var="temperature", names="city", title="Temperature Distribution")
# Pie chart - snapshot at one time
xpx(ds.isel(time=-1)).pie(var="temperature", names="city", title="Temperature Distribution")
Combining Slot Assignments¶
Mix explicit assignments with auto-assignment:
In [15]:
Copied!
# Explicit: variable -> facet_col, let city auto-assign to color
xpx(ds).line(facet_col="variable", color="city")
# Explicit: variable -> facet_col, let city auto-assign to color
xpx(ds).line(facet_col="variable", color="city")
In [16]:
Copied!
# Skip color slot with None
xpx(ds).line(var="temperature", color=None)
# Skip color slot with None
xpx(ds).line(var="temperature", color=None)