Since I’m interested in systemically creating such subplots with varying numbers of rows/columns, I want to place insets in their desired locations dynamically.
IIUC, assuming num_of_subplot_axes = rows * cols
, the axes of the first inset are current_inset_axes = num_of_subplot_axes + 1
, and the insets’ axes are in row-major ordering, left to right.
Code example
rows = 2
cols = 3
num_of_subplot_axes = rows * cols
current_inset_axes = num_of_subplot_axes + 1
fig = make_subplots(
rows=rows,
cols=cols,
vertical_spacing=0.05,
insets=[
dict(cell=(1, 1), l=0.55, b=0.43),
dict(cell=(1, 2), l=0.55, b=0.43),
dict(cell=(1, 3), l=0.55, b=0.43),
dict(cell=(2, 1), l=0.5, h=0.65, b=0.1),
dict(cell=(2, 2), l=0.5, h=0.65, b=0.1),
],
)
# add subplots
fig.add_trace(
go.Scatter(x=[0, 1, 2], y=[1.2, 0.2, 0.4], name="scatter (1,1)"), row=1, col=1
)
fig.add_trace(
go.Scatter(x=[0, 1, 2], y=[1.2, 0.2, 0.4], name="scatter (1,2)"), row=1, col=2
)
fig.add_trace(
go.Scatter(x=[0, 1, 2], y=[1.2, 0.2, 0.4], name="scatter (1,3)"), row=1, col=3
)
fig.add_trace(
go.Scatter(
x=[2, 3, 4, 5],
y=[1, 0.45, 1.277, 1.1],
mode="markers",
marker_size=15,
marker_color="brown",
name="scatter (2,1)",
),
row=2,
col=1,
)
fig.add_trace(
go.Scatter(
x=[2, 3, 4, 5],
y=[1, 0.45, 1.277, 1.1],
mode="markers",
marker_size=15,
marker_color="brown",
name="scatter (2,2)",
),
row=2,
col=2,
)
# add insets
fig.add_trace(
go.Bar(
x=["A", "B", "C"],
y=[0.8, 1.15, 0.76],
xaxis=f"x{current_inset_axes}",
yaxis=f"y{current_inset_axes}",
name="inset in (1,1)",
)
)
current_inset_axes += 1
fig.add_trace(
go.Bar(
x=["E", "F", "G", "BLAH"],
y=[0.8, 1.15, 0.76, 0],
xaxis=f"x{current_inset_axes}",
yaxis=f"y{current_inset_axes}",
name="inset in (1,2)",
)
)
current_inset_axes += 1
fig.add_trace(
go.Bar(
x=["E2", "F3", "G4", "BLAH"],
y=[0.5, 34, 0.76, 0.67],
xaxis=f"x{current_inset_axes}",
yaxis=f"y{current_inset_axes}",
name="inset in (1,3)",
)
)
current_inset_axes += 1
fig.add_trace(
go.Bar(
x=["H"],
y=[0.8],
xaxis=f"x{current_inset_axes}",
yaxis=f"y{current_inset_axes}",
name="inset in (2,1)",
)
)
current_inset_axes += 1
fig.add_trace(
go.Bar(
x=["I", "J"],
y=[0.8, 1.15, 0.76],
xaxis=f"x{current_inset_axes}",
yaxis=f"y{current_inset_axes}",
name="inset in (2,2)",
)
)
fig.update_layout(width=650, height=600, font_size=10)
fig.show()