Many subplots with one sublot consisting of a secondary y axis

I was trying to extend the following example 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.

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> my_plot.plot.layout
layout with fields annotations, margin, template, xaxis, xaxis2, yaxis, yaxis2, and yaxis3

However when I create 3 subplots

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> 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.

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.

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:

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)

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

Notice the line with secondary_y = false:

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:

In both cases, the 2nd trace is missing.

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

1 Like