# Many subplots with one sublot consisting of a secondary y axis

**URL:** https://discourse.julialang.org/t/many-subplots-with-one-sublot-consisting-of-a-secondary-y-axis/120827
**Category:** Visualization
**Tags:** plotlyjs
**Created:** [October 2, 2024, 9:51pm UTC](https://discourse.julialang.org/t/many-subplots-with-one-sublot-consisting-of-a-secondary-y-axis/120827 "2024-10-02T21:51:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![AUK1939](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@AUK1939](https://discourse.julialang.org/u/AUK1939)
#### Post date: [October 2, 2024, 9:51pm UTC](https://discourse.julialang.org/t/many-subplots-with-one-sublot-consisting-of-a-secondary-y-axis/120827/1 "2024-10-02T21:51:25Z")

</div>

I was trying to extend the following [example](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/4) to incude more subplots

I want to create subplots in which one subplot has a secondary y-axis.

Everything works fine in the 2 sub plot case.

```julia
my_plot = PlotlyJS.make_subplots(
    rows=2, cols=1,
    vertical_spacing=0.05, shared_xaxes=true, 
    specs=hcat([Spec(kind="xy", secondary_y = false), 
                Spec(kind="xy", secondary_y = true)]),
);

```

This works as expected, 3 y-axes are created (1 extra for the secondary y-axis)

```julia
julia> my_plot.plot.layout
layout with fields annotations, margin, template, xaxis, xaxis2, yaxis, yaxis2, and yaxis3

```

However when I create 3 subplots

```julia
my_plot_2 = PlotlyJS.make_subplots(
    rows=3, cols=1,
    vertical_spacing=0.05, shared_xaxes=true, 
    specs=hcat([Spec(kind="xy", secondary_y = false), 
                Spec(kind="xy", secondary_y = true), 
                Spec(kind="xy", secondary_y = false)]),
);

```

the fourth y-axis (i.e. the secondary y-axis of the 2nd subplot is not created)

```julia
julia> my_plot_2.plot.layout
layout with fields annotations, margin, template, xaxis, xaxis2, xaxis3, yaxis, yaxis2, and yaxis3

```

Is this a bug or have I am I missing something?

I have noticed if I make set up the subplots so the last subplot has a secondary y-axis then as expected 4 y axes are created.

```julia
my_plot_3 = PlotlyJS.make_subplots(
    rows=3, cols=1,
    vertical_spacing=0.05, shared_xaxes=true, 
    specs=hcat([Spec(kind="xy", secondary_y = false), 
                Spec(kind="xy", secondary_y = false), 
                Spec(kind="xy", secondary_y = true)]),
);

julia> my_plot_3.plot.layout
layout with fields annotations, margin, template, xaxis, xaxis2, xaxis3, yaxis, yaxis2, yaxis3, and yaxis4

```

Any other variation doesn’t create the extra y-axes and any subsequent plotting is impacted.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [November 7, 2024, 1:42am UTC](https://discourse.julialang.org/t/many-subplots-with-one-sublot-consisting-of-a-secondary-y-axis/120827/2 "2024-11-07T01:42:03Z")

</div>

> [@AUK1939](#):
>
> the fourth y-axis (i.e. the secondary y-axis of the 2nd subplot is not created)

Wouldn’t the fourth y-axis be the primary y-axis of the 3rd subplot?

Does the following reproduce what you see as a bug? Consider this example:

```julia
my_plot_2 = PlotlyJS.make_subplots(
    rows=3, cols=1,
    vertical_spacing=0.05, shared_xaxes=true, 
    specs=hcat([Spec(kind="xy", secondary_y = false), 
                Spec(kind="xy", secondary_y = true), 
                Spec(kind="xy", secondary_y = false),
                ]),
);

hdl_trace1 = PlotlyJS.scatter(x=[0, 1], y=[1, 2], name="1st")
hdl_trace2 = PlotlyJS.scatter(x=[1, 3], y=[2, 3], name="2nd")
hdl_trace3 = PlotlyJS.scatter(x=[1, 3], y=[2, 1], name="3rd")

PlotlyJS.add_trace!(my_plot_2, hdl_trace1, row = 1, col = 1)
PlotlyJS.add_trace!(my_plot_2, hdl_trace2, row = 2, col = 1, secondary_y = false)
PlotlyJS.add_trace!(my_plot_2, hdl_trace3, row = 3, col = 1)

PlotlyJS.relayout!(my_plot_2, 
    yaxis_title = "y-axis 1", 
    yaxis2_title = "y-axis 2",
    yaxis3_title = "y-axis 3",
    yaxis4_title = "y-axis 4",
)

PlotlyJS.display(my_plot_2)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/27127dd72723dba0e12170f9e91be166523d10d5.png)

Note that the last row is blank and missing the plot.

Notice the line with `secondary_y = false`:

```julia
PlotlyJS.add_trace!(my_plot_2, hdl_trace2, row = 2, col = 1, secondary_y = false)

```

If I change that to `secondary_y = true`, then the second row is blank:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/e/be6bcdf8d905427c2fe8f951da5a6922331aed1e.png)

In both cases, the 2nd trace is missing.

---

<div class="post-metadata">

### Author: ![AUK1939](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@AUK1939](https://discourse.julialang.org/u/AUK1939)
#### Post date: [November 7, 2024, 3:30am UTC](https://discourse.julialang.org/t/many-subplots-with-one-sublot-consisting-of-a-secondary-y-axis/120827/3 "2024-11-07T03:30:03Z")

</div>

Hi @jd-foster, That’s correct. Typo on my end, this should read “the fourth y-axis (i.e. the secondary y-axis of the 3nd subplot is not created)”.

And yes this is the bug I unearthed. However I wasn’t a 100% sure if this was indeed a bug or I was doing something wrong
