qtr = (
kpi.assign(
quarter=kpi["price_time"].dt.tz_localize(None).dt.to_period("Q").dt.start_time
)
.groupby("quarter")
.agg(observations=("price", "size"), markets=("market_key", "nunique"))
.reset_index()
)
qtr = qtr[qtr["quarter"] >= "2024-01-01"]
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_bar(
x=qtr["quarter"],
y=qtr["observations"],
name="price observations",
marker_color=BLUE,
opacity=0.55,
)
fig.add_scatter(
x=qtr["quarter"],
y=qtr["markets"],
name="distinct markets",
mode="lines",
line=dict(color=RUST, width=2),
secondary_y=True,
)
fig.update_layout(
title="Corporate-KPI market activity by quarter",
template="plotly_white",
height=420,
hovermode="x unified",
bargap=0.1,
legend=dict(orientation="h", y=1.12),
)
fig.update_yaxes(title="observations / quarter", secondary_y=False, showgrid=False)
fig.update_yaxes(title="distinct markets", secondary_y=True, showgrid=False)
fig.show()