# PlotlyJS: subplots and individual layouts

**URL:** https://discourse.julialang.org/t/plotlyjs-subplots-and-individual-layouts/87740
**Category:** General Usage
**Tags:** plotting, plotlyjs
**Created:** [September 24, 2022, 12:04pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-and-individual-layouts/87740 "2022-09-24T12:04:08Z")
**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: [September 24, 2022, 12:04pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-and-individual-layouts/87740/1 "2022-09-24T12:04:08Z")

</div>

Hi,  
I am stuck with an apparently simple problem. I am using PlotlyJS v0.18.8, Julia 1.8.1. I have some time series. If I plot each one separately, I have no problem: all layout attributes are fully displayed. For example, the following code will produce the plot below:

```julia
function gov()
    trace10_1 = scatter(;x = Date.(period10_1), y = GDP_shares[213:302,4], 
		name = "G/GDP", line_color = "DarkRed", mode = "markers+lines", 
		marker_size = 6, marker_symbol = "circle", line_width = 0.3)

    layout10_1 = Layout(;
			shapes = shape10_1,
			title_text = "Government as % GDP", 
			title_x = 0.5,
			hovermode = "x",
        		yaxis_title = "% points",
        		#yaxis_range=[17.2, 21.5], 
        		titlefont_size = 16)

    fig10_1 = plot(trace10_1, layout10_1)
end
gov()

```

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

However, when I use the PlotlyJS syntax to produce subplots, some of the individual layout attributes passed to the plot will be lost. For example, it preserves the correct `title`, but the `hovermode="x"` and `shapes` are gone. The following code, and its associated plot (below), show this problem very clearly:

```julia
fig10_5 = [gov() invest()]

```

 ![b](https://global.discourse-cdn.com/julialang/original/3X/9/0/9043b0ec2f065458ceba726a8589c91f325e5270.png)

If I try to force the inclusion of those two layout attributes by using the `relayout!` functionality, I succeed with the `hovermode="x"`, but fail concerning `shapes`. I do not know why `hovermode="x"` appears now in both subplots, while `shapes` is only inserted in the first subplot. Code and plot below:

```julia
fig10_5 = [gov() invest()]
relayout!(fig10_5, Layout(hovermode="x", shapes = shape10_1))
fig10_5

```

 ![c](https://global.discourse-cdn.com/julialang/original/3X/9/1/91cf308cdcb15354c74c661a6ac31fdf7b866769.png)

The funny part is that I need those shapes on multiple subplots.

Any help will be very much appreciated. 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: [September 25, 2022, 2:26pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-and-individual-layouts/87740/2 "2022-09-25T14:26:09Z")

</div>

The problem in my previous entry was not reproducible. It was entirely dependent on my data.

Below I present an MWE that can be easily reproducible.

1 - Defining shapes (two rectangles):

```julia
shapes1 = rect([5, 25], [10, 30],
                0, 1; fillcolor = "red", opacity = 0.1, line_width = 0,
                xref="x", yref = "paper");

```

2 - Producing the two individual plots:

```julia
function noise1()
    trace1 = scatter(;x=1:40, y=randn(40), name = "some")
    layout1 = Layout(;
	    shapes = shapes1,
	    title_text = "Some noise", 
	    title_x = 0.5,
	    hovermode = "x",
        titlefont_size = 16)
    fig1 = plot(trace1, layout1)
end
noise1()

function noise2()
    trace2 = scatter(;x=1:40, y=rand(40), name = "more")
    layout2 = Layout(;
		shapes = shapes1,
		title_text = "More noise", 
		title_x = 0.5,
		hovermode = "x",
        titlefont_size = 16)
   fig2 = plot(trace2, layout2)
end
noise2()

```

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

3 - Creating the subplots

```julia
fig3= [noise1() noise2()]
relayout!(fig3, Layout(; hovermode="x", shapes = shapes1))
fig3

```

 ![b](https://global.discourse-cdn.com/julialang/original/3X/1/3/136477f30b35c6c4a37f3566ba5f78cfda56d82a.png)

The strange problem: why do `shapes` only appear in the first subplot? How can I force them to be in the second plot as well?  
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: [September 25, 2022, 5:52pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-and-individual-layouts/87740/3 "2022-09-25T17:52:07Z")

</div>

@VivMendes  
It seems that you are defining subplots using the obsolete style (used in versions \<1.8).  
Here is the code using the methods `make_subplots`, and `add_vrect!`:

```julia
using PlotlyJS
fig= make_subplots(rows=1, cols=2, subplot_titles=["Some noise" "More noise"])
add_trace!(fig, scatter(;x=1:40, y=rand(40), name = "some"), row=1, col=1)
add_trace!(fig, scatter(;x=1:40, y=rand(40), name = "more"), row=1, col=2)
relayout!(fig, hovermode="x")
add_vrect!(fig, 5, 10, fillcolor = "red", opacity = 0.2, line_width = 0; row=1, col=1)
add_vrect!(fig, 20, 26, fillcolor = "blue", opacity = 0.2, line_width = 0; row=1, col=2) 
display(fig)

```

 ![subplot-shapes](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b5e9c8c8a957cb1719784f234817c1021551767.png)

For details search:

```julia
?make_subplots
?add_vrect!

```

LE: to add x-title for each subplot insert in relayout! the two corresponding kwargs and their values:

```julia
relayout!(fig, hovermode="x", xaxis_title="first x-title", xaxis2_title="second x-title")

```

---

<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: [September 25, 2022, 6:51pm UTC](https://discourse.julialang.org/t/plotlyjs-subplots-and-individual-layouts/87740/4 "2022-09-25T18:51:26Z")

</div>

@empet, thank you so much. Not only it works, but it is also much more intuitive than my version.

Yes, this is an old Pluto notebook I have to use again this year. My code did not work in the past, but I have decided not to let it go this time. You solved the problem.

As far as the official documentation is concerned, thanks to you, I have now realized that one thing is the version still available on the PlotlyJS website [here](http://juliaplots.org/PlotlyJS.jl/stable/), and the other is the one available on GitHub [here](https://github.com/JuliaPlots/PlotlyJS.jl/blob/master/examples/subplots.jl). Gosh, such a fantastic package, but maintenance seems to be complicated.

Thanks a lot.
