# Double y-axes in a sub-plot

**URL:** <https://discourse.julialang.org/t/double-y-axes-in-a-sub-plot/82186>\
**Category:** General Usage\
**Tags:** question, plotting, plots\
**Created:** [June 3, 2022, 1:15pm UTC](https://discourse.julialang.org/t/double-y-axes-in-a-sub-plot/82186 "2022-06-03T13:15:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vautrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vautrin/32/36729_2.png) [@Vautrin](https://discourse.julialang.org/u/Vautrin)\
**Post date:** [June 3, 2022, 1:15pm UTC](https://discourse.julialang.org/t/double-y-axes-in-a-sub-plot/82186/1 "2022-06-03T13:15:04Z")

</div>

I would like to have dual y-axes in one of my subplots; however, it seems like I am missing something about `twinx()` and `link = :x`, but I couldn’t figure out what…

Plotting double y-axis using `twinx()` works fine in a standard plotting frame. However, it creates the following problem when I try to link the x-axes. What am I missing here?

```julia
using Plots

x = [3, 5, 7, 9];
y = [10, 20, 30 , 40];
z = [-10, -20, -30, -40];

a = plot(1:10, color = :black, legend = false)

b = plot(x, y, color = :green, legend = false)
b = scatter!(twinx(), x, z, color = :orange, legend = false)

plot(a, b, layout = (2, 1), link= :x)

```

Output:  
 ![fig](https://global.discourse-cdn.com/julialang/original/3X/4/9/49a1c365aa9f0b1434fe37247012b5f65c942051.png)

Thanks very much!

---

<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:** [June 3, 2022, 2:19pm UTC](https://discourse.julialang.org/t/double-y-axes-in-a-sub-plot/82186/3 "2022-06-03T14:19:50Z")

</div>

This fixes two issues (_small margins and x-ticks labels scrambled_):

```julia
using Plots, Measures

x = [3, 5, 7, 9];
y = [10, 20, 30 , 40];
z = [-10, -20, -30, -40];

a = plot(1:10, color = :black, legend = false)

b = plot(x, y, color=:green, legend=false, margin=10mm)
scatter!(twinx(), x, z, color=:orange, legend=false, xlims=xlims(a))

plot(a, b, layout = (2, 1), link=:x, tickfontcolor=:black)

```

 ![Plots_gr_Measures_twinx_linkx_subplots](https://global.discourse-cdn.com/julialang/original/3X/c/3/c3f334e6ba2739f344bb499dd4dd6247401bb109.png)

---

<div class="post-metadata">

**Author:** ![Vautrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vautrin/32/36729_2.png) [@Vautrin](https://discourse.julialang.org/u/Vautrin)\
**Post date:** [June 3, 2022, 2:51pm UTC](https://discourse.julialang.org/t/double-y-axes-in-a-sub-plot/82186/4 "2022-06-03T14:51:17Z")

</div>

I am extremely grateful! Thanks very much for your time!
