# Plots: adding plots to \`twinx()\` axis

**URL:** <https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [November 14, 2023, 3:06pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223 "2023-11-14T15:06:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2023, 3:06pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/1 "2023-11-14T15:06:59Z")

</div>

I’m trying to construct a plot using the `Plots` package with a normal axis and a `twinx()` axis. Then I want to add plots to both axes, but it doesn’t seem to work:

```julia
using Plots
plot(sin,lc=:red, legend=:left)
fg = plot!(twinx(),sinc,lc=:blue,legend=:right)
plot!(fg, cos, lc=:red, ls=:dash)

```

gives:

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

Next, I want to add a plot to the `twinx()` axis, and I try with:

```julia
plot!(fg,twinx(),cosc,lc=:blue, ls=:dash)

```

but this gives an error message.

**Question:** Is there a way to add plots to the `twinx()` axis, too? [I’m trying to combine particle trajectory plots using Monte Carlo simulation…]

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 14, 2023, 3:17pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/2 "2023-11-14T15:17:52Z")

</div>

> [@BLI](#):
>
> twinx()

Assign this to a variable instead of making a new function call.  
See this related [thread and solution](https://discourse.julialang.org/t/dual-axis-plot-breaks-with-multiple-variables-on-right-secondary-y-axis/64491/4).

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2023, 3:33pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/3 "2023-11-14T15:33:18Z")

</div>

> [@rafael.guerra](#):
>
> Assign this to a variable instead of making a new function call.

Still not 100% intuitive… But I found a work-around:

```julia
using Plots
yaxis2 = twinx()
plot(sin,lc=:red, legend=:left)
fg = plot!(yaxis2,sinc,lc=:blue,legend=:right)
plot!(fg, cos, lc=:red, ls=:dash)

```

To add the second plot to `yaxis2`, the following does _not_ work:

```julia
plot!(fg,yaxis2,cosc, lc=:blue, ls=:dash)

```

However, the following works:

```julia
plot(fg)
fg = plot!(yaxis2,cosc, lc=:blue, ls=:dash)

```

Thanks!

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2023, 4:40pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/4 "2023-11-14T16:40:48Z")

</div>

Hm… upon further testing, there is something seriously weird with the above solution… or something that I just don’t understand. I’m trying to find a minimal example, but it seems like there is a memory problem/memory leak:

- I have a lengthy Jupyter notebook with lots of (MTK) simulations, and try to plot the results. When I use the above procedure, some previous plots “leak” into the new plot.

I have to check more.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 14, 2023, 5:33pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/5 "2023-11-14T17:33:14Z")

</div>

Check the following logic, I didn’t encounter any problems:

```julia
using Plots
yaxis1 = plot(sin, lc=:red, legend=:left, x_foreground_color_text=:black, y_foreground_color_text=:red)
yaxis2 = twinx()
plot!(yaxis2, sinc, lc=:blue, legend=:right, y_foreground_color_text=:blue)
plot!(yaxis2, cos, lc=:blue, ls=:dash)
plot!(yaxis1, cosc, lc=:red, ls=:dash)

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 14, 2023, 7:11pm UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/6 "2023-11-14T19:11:47Z")

</div>

Note: my problem is not with my code above, but with using that strategy on a much more complex case.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [November 15, 2023, 10:19am UTC](https://discourse.julialang.org/t/plots-adding-plots-to-twinx-axis/106223/7 "2023-11-15T10:19:04Z")

</div>

OK… I _think_ I understand the logic you present, and from that it follows that I had misunderstood what the `twinx()` function does.

So, if I understand it _correctly_, the `twinx()` creates a second axis of the _previously displayed plot_. And by setting `yaxis2 = twinx()`, you define a “plot handle” (in MATLAB terminology) to this second axis of that plot.

It was still somewhat convoluted to add `xlabel` and `ylabel` for the plots… Here is the plot I _finally_ managed to create, after quite a bit of testing:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/d/fdf7dfa6a1158ee3447e4fed4b57edfb96ef0dce.png)

It is not relevant here to go into too much details, but here is more or less what I had to do… [note: I also created another plot in parallel with the one included above]:

```julia
# Preparing for the figure included above ("Voltages") ...
fg_1 = plot(sol,idxs=u, xlabel="",ylabel=L"$u$ [V]", title="Voltages",
                label=L"\leftarrow u", legend=:left)
fg_2 = twinx()
plot!(fg_2, sol,idxs=u_p, ylabel=L"$u_\mathrm{p}$ [V]",
        label=L"u_\mathrm{p} \rightarrow", legend=:right)

# Aother figure...

fg_a = plot(sol, idxs=p,...)

# Monte Carlo simulation
Np = 20

for i in 1:Np
    _u = 0.2*randn()
    _k = 1 + 0.2*randn()
    prob = remake(prob; u0=[u => _u], p=[k => _k])
    sol = solve(prob)

    plot!(fg_1 , sol, idxs=u, label="", xlabel="")
    plot!(fg_2 , sol, idxs=u_p, label="", xlabel="")

    plot!(fg_a,sol, idxs=p, ...)
end

plot!(fg_2, xlabel=L"$t$ [s]")
plot!(fg_1, frame=:box)

```

Note: if I replace the last two statements by `plot!(fg_1, xlabel=L"$t$ [s]", frame=:box)`, this produces _two_ labels on the abscissa (“x axis”). I had to add `ylabel`’s prior to the `for` loop in order to get them correctly placed. Etc., etc.

So: somewhat convoluted.
