Make_subplots(): Two rows, One column

Yesterday it gave me an headache, why I can not plot two figures on top of each other,
the problem is related to the topic:
how-to-create-a-2x1-array.

Here is my example:

using PlotlyJS

hdl_trace1 = scatter(x=[1, 2], y=[1, 2], name="(1,1)")
hdl_trace2 = scatter(x=[1, 2], y=[2, 3], name="(1,2)")

hdl_plt = make_subplots(rows=2, cols=1, specs = reshape([ Spec(kind="xy") Spec(kind="xy")], :, 1) )
hdl_plt = make_subplots(rows=2, cols=1, specs = permutedims([ Spec(kind="xy") Spec(kind="xy")]))

println(string("window_size = ", size(hdl_plt)  )  )

add_trace!(hdl_plt, hdl_trace1, row=1, col=1)
add_trace!(hdl_plt, hdl_trace2, row=2, col=1)

relayout!(hdl_plt, title_text="Example subplots: two row by one colum plot")
display(hdl_plt)

Is this a question or an example of a solution?

Hi, thanks for your feedback!
The related question could be: What is wrong here?

using PlotlyJS
hdl_plt = make_subplots(rows=2, cols=1, specs = [ Spec(kind="xy") Spec(kind="xy")])

I agree, it is somewhat unintuitive as the linked post makes clear. One might expect this to work:

hdl_plt = make_subplots(rows=2, cols=1, specs = [ Spec(kind="xy") ; Spec(kind="xy")])

Notice I’ve added a semicolon between, to create a 2-element Vector{Spec} to correspond tor 2 rows with 1 column. But still spec needs to be a Matrix type, while a Vector type causes an error! It is surprising.

You can also use missing “to move past a col/row span” as is says under spec in the help of make_subplots:

hdl_plt = make_subplots(rows=2, cols=1, specs = [ Spec(kind="xy") missing; Spec(kind="xy") missing])

In fact I also began yesterday to play around with the placeholder “missing”, but I tried this trick only inside a large script, not suitable to be shared.
The strange thing is, a “poor mens solution” that works inside this large script, fails inside a minamal example.
Therefore, the next two questions:

  • does the example work on your machine?
  • if it fails, what is wrong? why does this work in a large script but not in an example?
using PlotlyJS
hdl_trace1 = PlotlyJS.scatter(x=[1, 2], y=[1, 2], name="(raw#1, col#1)")
hdl_trace2 = PlotlyJS.scatter(x=[1, 2], y=[2, 3], name="(raw#2, col#1)")
hdl_plt = [hdl_trace1; hdl_trace2]
PlotlyJS.relayout!(hdl_plt, title_text = "Example subplots: Simple version two row by one colum plot")
hdl_plt = PlotlyJS.display(hdl_plt)

This example doesn’t work with a MethodError for relayout!. Here is how I would do this:

using PlotlyJS
hdl_plot1 = PlotlyJS.Plot(PlotlyJS.scatter(x=[1, 2], y=[1, 2], name="(row#1, col#1)"))
hdl_plot2 = PlotlyJS.Plot(PlotlyJS.scatter(x=[1, 2], y=[2, 3], name="(row#2, col#1)"))
hdl_plt = [hdl_plot1; hdl_plot2]

PlotlyJS.relayout!(hdl_plt, title_text = "Example subplots: Simple version two row by one column plot")
hdl_plt = PlotlyJS.display(hdl_plt)

Here I’ve wrapped the traces in the Plot() function before using the concatenation syntax [hdl_plot1; hdl_plot2].