Highlighting Time Series Regions with Rectangle Shapes
Mix.install([
{:plotly_ex, "~> 0.3"},
{:kino, "~> 0.18"}
])
Section
Add layout.shapes — a list of shape maps — to overlay geometric shapes on
a chart. Set xref: "x" to anchor the x bounds to data coordinates and
yref: "paper" to span the full height (0 = bottom, 1 = top) regardless of
y scale. Use line: %{width: 0} to suppress the border.
alias Plotly.{Figure, Scatter}
Figure.new()
|> Figure.add_trace(
Scatter.new(
x: ["2015-02-01", "2015-02-02", "2015-02-03", "2015-02-04",
"2015-02-05", "2015-02-06", "2015-02-07", "2015-02-08",
"2015-02-09", "2015-02-10"],
y: [1, 3, 2, 4, 5, 3, 4, 6, 5, 7],
mode: "lines"
)
)
|> Figure.update_layout(
title: "Highlighted Time Regions",
shapes: [
%{
type: "rect",
xref: "x",
yref: "paper",
x0: "2015-02-04",
y0: 0,
x1: "2015-02-06",
y1: 1,
fillcolor: "#d3d3d3",
opacity: 0.5,
line: %{width: 0}
},
%{
type: "rect",
xref: "x",
yref: "paper",
x0: "2015-02-08",
y0: 0,
x1: "2015-02-10",
y1: 1,
fillcolor: "#d3d3d3",
opacity: 0.5,
line: %{width: 0}
}
]
)
|> Plotly.show()