# Issue with 2 y axis using twinx with subplots using Plots.jl

**URL:** <https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [March 29, 2022, 7:59pm UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696 "2022-03-29T19:59:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Post date:** [March 29, 2022, 7:59pm UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696/1 "2022-03-29T19:59:51Z")

</div>

Hi everyone,

I am trying to make a very simple plot using Plots and I can’t make it work.  
My goal is to use layout to have 2 subplots. The first 1 has to have two different y axis, using twinx I assume. My problem is that I don’t know how to do that with the layout idea. Here is my attempt with a simple case:

```julia
x = 0:0.1:1

l = @layout [a b]
p1 = plot(cos.(x), label="1",right_margin=5Plots.mm)
plot!(twinx(), sin.(x), label="2")
p2 = plot(tan.(x), label="3")
plt = plot(p1, p2, layout = l)

```

which gives:  
 ![plot_28](https://global.discourse-cdn.com/julialang/original/3X/4/9/4909dfdc2f37f2418212cd59720b7caf3200d22f.png)

There are 2 problems: First I don’t see the label of cos(x) and 2nd, we see well that twinx is overlapping the first plot, with the x labels beeing kinda in bold.

Is there another simpler alternative?

Thx!

---

<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:** [March 30, 2022, 6:50am UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696/2 "2022-03-30T06:50:10Z")

</div>

One way - no bells & whistles:

```julia
using Measures, Plots; gr()

x = 0:0.1:1
l = @layout [a b]
p1 = plot(cos.(x), lc=:red, legend=:topleft, label="1",right_margin=5mm, ylims=(0.5, 1.1))
plot!(twinx(), sin.(x), lc=:blue, legend=:topright, label="2", ylims=(0.0, 1.0))
p2 = plot(tan.(x), label="3", ylims=(0.0, 1.8))
plt = plot(p1, p2, layout = l)

```

 ![Plots_gr_twinx_legends](https://global.discourse-cdn.com/julialang/original/3X/5/8/5813ccb0c8d8d0db78dcdfe970e560e15249316b.png)

---

<div class="post-metadata">

**Author:** ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Post date:** [March 30, 2022, 9:34am UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696/3 "2022-03-30T09:34:44Z")

</div>

> [@rafael.guerra](#):
>
> `using Measures, Plots; gr()`

Thx, I see what you did. So it doesn’t seem possible to have the legends together in the same square in a simple way.  
I still have the problem of bold xaxis on my computer though:

 ![Capture d’écran 2022-03-30 à 10.20.50](https://global.discourse-cdn.com/julialang/original/3X/1/a/1aad4d6fe5f705faf8d15624f606cbe860196f90.png)

But that would do for now!

---

<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:** [March 30, 2022, 10:20am UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696/4 "2022-03-30T10:20:23Z")

</div>

> [@Iddingsite](#):
>
> So it doesn’t seem possible to have the legends together in the same square in a simple way

Everything is possible:

 ![Plots_gr_twinx_legends2](https://global.discourse-cdn.com/julialang/original/3X/4/8/48dfa8fdb349062bc357786eca3bde5e018f1811.png)

```julia
using Measures, Plots; gr()

x = 0:0.1:1
l = @layout [a b]
p1 = plot(x, cos.(x), lc=:red, legend=:topleft, label="1",right_margin=5mm, ylims=(0.5, 1.1))
plot!(twinx(), x, sin.(x), ylims=(0.0, 1.0), lc=:blue, label=false)
plot!([-1],[0], xlims=extrema(x), lc=:blue, label="2")
p2 = plot(x, tan.(x), label="3", ylims=(0.0, 1.8))
plt = plot(p1, p2, layout = l)

```

---

<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:** [March 30, 2022, 10:22am UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696/5 "2022-03-30T10:22:20Z")

</div>

As for the overwriting of the x-ticks making it look bold, I do not see it with “Computer Modern” fonts.

---

<div class="post-metadata">

**Author:** ![Iddingsite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iddingsite/32/31840_2.png) [@Iddingsite](https://discourse.julialang.org/u/Iddingsite)\
**Post date:** [March 30, 2022, 5:04pm UTC](https://discourse.julialang.org/t/issue-with-2-y-axis-using-twinx-with-subplots-using-plots-jl/78696/6 "2022-03-30T17:04:12Z")

</div>

Thx for the small tricks, that is what I was looking for! For the font, it is ok at high resolution, so it works for me 👍

Thank you for your help!
