How do I add annotations to a specific sub-plot in a figure in Julia?

Hi All,

I am trying to add annotations to sub plots in a figure but there doesn’t seem to be a way of specifying which sub-plot the annotation appears in. It always appears in the first sub-plot.

How can I specify the sub plot in which to place the annotation?

Here is an example in Julia and the result:

##########
using Plots
using PlotlyJS
const pjs = PlotlyJS; # Create and alias for PlotlyJS

# generate some test data
theta = -pi:pi/100:pi

# create the traces
tan_plt = pjs.scatter(x=θ, y=tan.(θ), name="tan(θ)");
sin_plt = pjs.scatter(x=θ, y=sin.(θ), name="sin(θ)");
cos_plt = pjs.scatter(x=θ, y=cos.(θ), name="cos(θ)");

# create a figure with 3 sub_plots to hold the traces
trigfig = make_subplots(rows=3, cols=1, shared_xaxes=true, 
                    row_heights=[0.25, 0.25,0.5], vertical_spacing=0.01);

# add the traces to the figure, one in each sub-plot
add_trace!(trigfig, tan_plt, row=1, col=1);
add_trace!(trigfig, sin_plt, row=2, col=1);
add_trace!(trigfig, cos_plt, row=3, col=1);

# create some annotations
annots = [
   attr(x=pi/2, y=0,
       text="π/2",
       showarrow=true,  arrowcolor="limegreen",
       arrowhead=1, arrowwidth=3,
       font=attr(
                family="Courier New, monospace",
                size=16,
                color="limegreen"
            )
   ),
];

# Update the figure and add annotations
relayout!(trigfig, width=640, height=480, title="Basic trig functions",
          yaxis_title="tan(θ)", yaxis2_title="sin(θ)", yaxis3_title="cos(θ)",
          yaxis_type=:log,
          plot_bgcolor=:lightgray, paper_bgcolor=:lightgray,
          annotations=annots
);
          
#display the plot
trigfig

Here is the result

trig plot

Hi @lcabey,
For subplots you can set the annotation position through xref, respectively yref.
See https://plotly.com/julia/reference/layout/annotations/#layout-annotations-items-annotation-yref.
In your case since you have shared xaxes, the subplot:

  • (1,1) is referenced with respect to xaxis “x”, yaxis “y”,
  • (1,2) is referenced with respect to xaxis “x”, yaxis “y2”,
  • (1,3) is referenced with respect to xaxis “x”, yaxis “y3”.
    If you want to display that annotation in the subplot (1,3), then define:
annots = [
   attr(x=pi/2, y=0,
        xref="x",
        yref="y3",
       text="π/2",
       showarrow=true,  arrowcolor="limegreen",
       arrowhead=1, arrowwidth=3,
       font=attr(
                family="Courier New, monospace",
                size=16,
                color="limegreen"
            )
   ),
];

To understand the difference between the three ways to define xref, yref I paste here two examples:

using PlotlyJS

tr=scatter(x=[0,2,3], y=[0.5, -0.75, 1.63])

annots=[attr(x=1,#annot placed at the point of coords(x,y)=(1,1)  with respect to axes
            y=1,
            xref="x",
            yref="y",
            text="refs 'x', 'y'<br>(1,1)",
            showarrow=false,
            align="center"),
        attr(x=1, #annot places in the upper-right corner of the plot area
            y=1,
            xref="paper",
            yref="paper",
            text="refs paper<br>(1,1)",
            showarrow=false),
        attr(x=1.15, #annot outside plot area (at right of it, i.e. x>1)
            y=0.4,
            xref="paper",
            yref="paper",
            text="refs paper<br>(1.15,0.4)",
            showarrow=false,
            align="right",
            xanchor="right"),
        attr(x=0.5,#annot outside plot area (above it, i.e. y>1)
            y=1.2,
            xref="paper",
            yref="paper",
            text="refs paper<br>(0.5,1.2)",
            showarrow=false,
            align="right"),
        attr(x=0,#annot outside plot area (below  it, i.e y<0)
            y=-0.25,
            xref="paper",
            yref="paper",
            text="refs paper<br>(0,-0.25)",
            showarrow=false)]


pl = Plot(tr, Layout( width=600, height=500, xaxis_range=[-1, 4],
                      margin=attr(t=150, b=85, r=85),
                     showlegend=false, annotations=annots))

annots-x-paper

and this one for subplots

fig= make_subplots(rows=1, cols=2)
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)
display(fig)

1 Like

Hi empet,

Thanks for your reply. It is exactly what I needed.
:+1:t6: :+1:t6: