# Configure a specific axis in a figure with multiple subplots

**URL:** https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014
**Category:** General Usage
**Tags:** plotlyjs
**Created:** [October 20, 2022, 2:47pm UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014 "2022-10-20T14:47:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 20, 2022, 2:47pm UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/1 "2022-10-20T14:47:58Z")

</div>

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

```julia
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
PlotlyJS.relayout!(hdl_plt, 
    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.display(hdl_plt)
PlotlyJS.savefig(hdl_plt, raw"c:\tmp\plt\two_plts.json")

```

---

<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: [October 20, 2022, 5:34pm UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/2 "2022-10-20T17:34:02Z")

</div>

With the last relayout! like this:

```julia
relayout!(hdl_plt, 
    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.

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 20, 2022, 9:23pm UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/3 "2022-10-20T21:23:38Z")

</div>

Thank you! I also figured out a third option:

```julia
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:

```julia
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?

---

<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: [October 21, 2022, 11:43am UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/4 "2022-10-21T11:43:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)
#### Post date: [October 21, 2022, 12:26pm UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/5 "2022-10-21T12:26:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 21, 2022, 1:44pm UTC](https://discourse.julialang.org/t/configure-a-specific-axis-in-a-figure-with-multiple-subplots/89014/6 "2022-10-21T13:44:47Z")

</div>

> [@stephancb](#):
>
> It would be possible to code a `merge_plots(fig1, ...)` function that merges stand-alone plots `fig1, ....` into a superplot

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.
