I need to create a figure with subplots using PlotlyJS. In particular, I need one with annotations and individual titles in the different subplots. I searched the internet and found an excellent example by @empet, which includes annotations, here.
@empte’s code and figure are below:
using PlotlyJS
fig= make_subplots(rows=1, cols=2,
subplot_titles= ["First subplot" "Second subplot"] )
add_trace!(fig, scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 3, 2, 4, 3, 4, 6, 5]),
row=1, col=1)
add_trace!(fig, scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 4, 5, 1, 2, 2, 3, 4, 2]),
row=1, col=2)
annotations =
[attr(x=0.6, #annot similar to ref "paper" when the fig contains only one subpl,
#but here within plot area of the first trace
y=0.75,
xref="x domain",
yref="y domain",
text="refs domain<br>(0.6,0.75)",
showarrow=false,
align="center"),
attr(x=0.6, #annot similar to ref "paper" when the fig contains only one subpl,
#but here within plot area of the second trace
y=0.75,
xref="x2 domain",
yref="y2 domain",
text="refs domain<br>(0.6,0.75)",
showarrow=false,
align="center"),
attr(x=0.5, #annot with respect to "paper", below the fig plot area
y=-0.15,
xref="paper",
yref="paper",
text="refs paper<br>(0.5,-0.15)",
showarrow=false,
align="center"),
attr(x=6, #annot with respect to xaxis 1, yaxis1
y=1.5,
xref="x1",
yref="y1",
text="refs x1, y1<br>(6,1.5)",
showarrow=false,
align="center"),
attr(x=4, #annot with respect to xaxis2, yaxis2
y=4.5,
xref="x2",
yref="y2",
text="refs x2, y2<br>(4,4.5)",
showarrow=false,
align="center",
)]
relayout!(fig, width=900, height=400, annotations=annotations)
fig
I can replicate @empet’s example. However, when I tried to add subplot_titles=["Subplot 1" "subplot 2"]
it did not work. If I force having subplot_titles,
I will not get the annotations,
and vice versa. What am I doing wrong here?
Help will be very much appreciated.
Thanks