Configure a specific axis in a figure with multiple subplots

Today it gave me a headache to understand how to plot two subplots, one with a single y-axis and another with two y-axis with specific axis titles. The logic seems to be, each subplot with one y-axis adds 1 y-axis and each subplot with two y-axis adds 2 y-axis
to the overall count of y-axis.
Below my example, I hope this helps others to understand the logic.
I do not know, if this is the recommended way to do the job in PlotlyJS, but it works for me (which is fine :slight_smile: ).

using PlotlyJS
# -------------------------------------------------------------------------------------------------
# Example to configure the title of a specific axis in a specific subplot: 
# -------------------------------------------------------------------------------------------------
hdl_trace1 = PlotlyJS.scatter(x=[0, 1], y=[1, 2], name="(r1, c1)")
hdl_trace2 = PlotlyJS.scatter(x=[1, 3], y=[2, 3], name="1st(r2, c1)")
hdl_trace3 = PlotlyJS.scatter(x=[1, 3], y=[2, 1], name="2nd(r2, c1)")
# ---  layout of plot with subplots:                             Column1
hdl_plt = PlotlyJS.make_subplots(rows=2, cols=1, specs = [  Spec(kind="xy", secondary_y = false)     missing;  # row 1
                                                            Spec(kind="xy", secondary_y = true)     missing])  # row 2
# --- define traces:
PlotlyJS.add_trace!(hdl_plt, hdl_trace1, row = 1, col = 1)
PlotlyJS.add_trace!(hdl_plt, hdl_trace2, row = 2, col = 1)
PlotlyJS.add_trace!(hdl_plt, hdl_trace3, row = 2, col = 1, secondary_y = true)
# --- comon title and size:
PlotlyJS.relayout!(hdl_plt, height=500, width=450, title_text = "Example subplots: two row by one colum plot")
# --- count number of yaxis, two subplots: a) one yaxis, b) two yaxis => 3 yaxis, yaxis3 needs to be formated
    yaxis =  PlotlyJS.attr(; title      = "y-axis <b>1</b>", ), 
    yaxis2 = PlotlyJS.attr(; title      = "y-axis <b>2</b>", ),
    yaxis3 = PlotlyJS.attr(; title      = "y-axis <b>3</b>", )) 
# --- show plot and safe plot as json:
PlotlyJS.savefig(hdl_plt, raw"c:\tmp\plt\two_plts.json")

With the last relayout! like this:

    yaxis_title = "y-axis <b>1</b>", 
    yaxis2_title = "y-axis <b>2</b>",
    yaxis3_title = "y-axis <b>3</b>") 

you’ll get the right axes titles.

1 Like

Thank you! I also figured out a third option:

hdl_plt.plot.layout.yaxis3[:title] = "3<sup>rd</sub> y-axis new"

What is annoying from my point of view, is the fact, that it does not seem to be possible
to configure a subplot as a stand alone subplot, e.g. something like:

PlotlyJS.Layout!(subplot_A, title_text= "my title text", xaxis_title= "my axis title for this subplot", ...)

I need to count the subplots and axis in use and if I add an axis I need to change the numbering of the following axis manually.
I guess this is a limitation of Plotly, am I right?

It’s your opinion that this is a limitation of Plotly, because perhaps you think in terms of another plotting library.
Plotly has a logical architecture: a clear separation between trace and layout attributes.
When a figure represents subplots, then, as in mathematics, each subplot is referenced to a system of axes, and axes have a name to be distinguished.
It is much simpler to set the global attributes of the figure, like width, height, font_family, font size, and
local attributes specific to each subplot, as axis attributes, than naming each subplot and setting its properties by using its name.

It would be possible to code a merge_plots(fig1, ...) function that merges stand-alone plots fig1, .... into a superplot, automatically numbering the axis, adding up the widths and heights, etc. So not really a limitation, rather something nobody has added yet to the code base.

That seem to be suitable approach. What I really like in Plotly is the option to put more than 2 y-axis on one single plot. But when I have an assembly of 4 subplots and on the first plot I remove an axis this could really be a challenge. And I do not know, if it is realistic to add to a subplot more than two y-axis, I would not know how to achieve this.