# PlotlyJS: subplots with subplot\_titles + annotations

**URL:** <https://discourse.julialang.org/t/plotlyjs-subplots-with-subplot-titles-annotations/106417>\
**Category:** Visualization\
**Tags:** plotting, plotlyjs\
**Created:** [November 18, 2023, 7:54pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-with-subplot-titles-annotations/106417 "2023-11-18T19:54:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [November 18, 2023, 7:54pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-with-subplot-titles-annotations/106417/1 "2023-11-18T19:54:02Z")

</div>

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](https://community.plotly.com/t/how-do-i-add-annotations-to-a-specific-sub-plot-in-a-figure-in-julia/61586/3).

@empte’s code and figure are below:

```julia
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

```

 ![a](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f5b4b72b7d53db0513b4a5fde6901368ac94318.png)

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

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [November 18, 2023, 8:34pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-with-subplot-titles-annotations/106417/2 "2023-11-18T20:34:04Z")

</div>

The subplot titles are added to layout as annotations.  
This line:

```julia
relayout!(fig, width=900, height=400,
                     annotations=annotations)

```

does not update annotations, but overwrites the existing ones, i.e. the subplot titles.  
In order to get the right plot, the first idea that comes to my mind is to proceed as follows:

```julia
relayout!(fig, width=900, height=400)
append!(fig.plot.layout.annotations, annotations)
display(fig)

```

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [November 18, 2023, 9:30pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-with-subplot-titles-annotations/106417/3 "2023-11-18T21:30:46Z")

</div>

@empet, thank you very much. Your little trick did the job. The knowledge you have about this stuff seems never to end. In Pluto, your solution:

 ![a](https://global.discourse-cdn.com/julialang/original/3X/1/1/118ab71d9dce7cf054c92e224cb20d68d286e356.png)

```julia
let
	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)
	append!(fig.Plot.layout.annotations, annotations)
	fig

end

```

A small note. In VScode, the same code that does the job in Pluto does not lead to the same result (maybe the reactivity of Pluto has advantages here). It produces two sequential plots:

 ![aa](https://global.discourse-cdn.com/julialang/original/3X/3/3/3312eff1147c9fc4d8521c0c10df9d901d926a77.png)

Anyway, the fact that it works in Pluto is what matters to me.

Thanks

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [November 19, 2023, 9:30pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-with-subplot-titles-annotations/106417/4 "2023-11-19T21:30:35Z")

</div>

@empet, contrary to what I mentioned in my previous post, using your code in a slightly different example works both in VScode and Pluto. In VScode:

```julia
using PlotlyJS

p = make_subplots(rows=2, cols=2, specs=[Spec(colspan=2) missing ; Spec() Spec()],
subplot_titles=["First subplot" "Third subplot" ; "Second subplot" missing],
vertical_spacing = 0.1, horizontal_spacing = 0.09 )

add_trace!(p, scatter(x=1962:2022, y = randn(61), mode = "markers+lines", marker_color="navy", 
            marker_size = 7, marker_symbol = "circle", line_width = 0.5 , linewitdth=0.3, 
            name = "Birds"), 
            row=1, col=1)
	
add_trace!(p, scatter(x=1962:2022, y = randn(61), mode = "markers+lines", marker_color="blue",
            marker_size = 5.5, marker_symbol = "circle", line_width = 0.5 , linewitdth=0.3, 
            name = "Cats"), 
			row=2, col=1)
	
add_trace!(p, scatter(x=1962:2022, y = randn(61), mode = "markers+lines", marker_color="maroon",
            marker_size = 5.5, marker_symbol = "circle", line_width = 0.5 , linewitdth = 0.3, 
			name = "Dogs" ), 
			row=2, col=2)

	annotations=[
		attr(x=1999 , y=2.5, text="One cat", showarrow=false, arrowhead=1 ),
        attr(x=2008 , y=2.5, text="One dog", showarrow=false, arrowhead=1 ),
        attr(x=2019 , y=2.5, text="One frog", showarrow=false, yshift=0 )]

	
relayout!(p, showlegend=true, title_text = "Figure with subplots and annotations", 
            height = "700", xaxis_range = [1960, 2023], hovermode = "x", yaxis_range=[-3 , 3])	
	
append!(p.plot.layout.annotations, annotations)

add_shape!(p, line( x0=2001, y0=-3, x1=2001, y1=3, line=attr(color="RoyalBlue", width=1.5, 
            dash="dashdot")), row=1, col=1)
	
add_shape!(p, line( x0=2008, y0=-3, x1=2008, y1=3, line=attr(color="RoyalBlue", width=1.5, 
            dash="dashdot")), row=1, col=1)
	
add_shape!(p, line( x0=2019, y0=-3, x1=2019, y1=3, line=attr(color="RoyalBlue", width=1.5, 
            dash="dashdot")), row=1, col=1)
p

```

with the plot:

 ![a](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e5db6bc9c910d3c0f8f55dbf25ddfa0a9cc8537.png)
