# Show right y-axis using twinx() with Plots pyplot()

**URL:** <https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750>\
**Category:** General Usage\
**Tags:** pyplot, plots\
**Created:** [November 5, 2019, 3:07pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750 "2019-11-05T15:07:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)\
**Post date:** [November 5, 2019, 3:07pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/1 "2019-11-05T15:07:34Z")

</div>

Hi,

I am using twinx() to overlay 2 graphs with different y-axis, but only the y-axis on the left shows up in the plot. On the right, I only see numbers. Also, the y-label on the right gets cut out.

Does anyone know how to fix it? Thank you.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 5, 2019, 5:09pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/2 "2019-11-05T17:09:02Z")

</div>

Could you post a minimum working example as described in [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)? It’s very hard to help you without knowing what you’re doing exactly, and what plotting package you’re actually using.

---

<div class="post-metadata">

**Author:** ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)\
**Post date:** [November 6, 2019, 11:45am UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/3 "2019-11-06T11:45:25Z")

</div>

Fair, sorry about that.

```julia
using Plots
pyplot()

a = rand(10)
myplot = plot(a,a,grid=false,color="green",linewidth=1,markershape=:circle,markerstrokecolor=:green,markersize=6,
     titlefont=f15, tickfont=f15, legendfont=f13,xguidefont=f15,yguidefont=f15,
    label="left1",legend=:topleft,size=(1000,1000))
plot!(a,0.5a,grid=false,color="purple",linewidth=1,markershape=:diamond,markerstrokecolor=:purple,markersize=6,
     titlefont=f15, tickfont=f15, legendfont=f14,xguidefont=f15,yguidefont=f15,
    label="left2")
yaxis!("Y1")
plt = twinx()
plot!(plt,a, -2a,grid=false,color="black",linewidth=2,
     titlefont=f15, tickfont=f15, legendfont=f13,xguidefont=f15,yguidefont=f15,ytickfont=f15,
    label="right1", legend=:topright,yaxis="Y2")
vline!(plt,[0.5],color="black",line=(:dot,2),label="")
```

---

<div class="post-metadata">

**Author:** ![Bastian\_Moya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bastian_moya/32/9516_2.png) [@Bastian\_Moya](https://discourse.julialang.org/u/Bastian_Moya)\
**Post date:** [February 6, 2020, 4:46am UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/4 "2020-02-06T04:46:56Z")

</div>

Did you solve it? I am trying to do something similar but i cannot add the y label.

---

<div class="post-metadata">

**Author:** ![juliauser](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@juliauser](https://discourse.julialang.org/u/juliauser)\
**Post date:** [February 15, 2020, 4:25pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/5 "2020-02-15T16:25:35Z")

</div>

Problem still there!

---

<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:** [February 15, 2020, 10:54pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/6 "2020-02-15T22:54:00Z")

</div>

Hm… your code is incomplete, so I cannot test it 100%. [You include some undefined `f13`, `f14`, and `f15` to specify fonts…]

A couple of other things: I’m used to using `ylabel` to specify y axis text, but both `ylabel` and `yaxis` seem to work.

Anyway, your problem is:

- The y-axis appears only on the left hand side, and on the right hand side, there is only numbers.
- The y-axis text disappears on the right hand side.

To get “y-axis” on both the left and right, you can probably add keyword + value `box=:true`. This doesn’t give y-axis, I guess, but draws a line all around the plots (i.e., also on the top). I don’t know if that is what you want.

The disappearing label on the right hand side y-axis… is that just a bug in the plotting window of IJulia, or is the label really missing? To test, save the figure to a file, and open the file in, say, Google Chrome, MS Edge (Chromium version), etc.

When I add the command `savefig("filename.svg")` after the last plot command in the same IJulia cell, the plot is saved to file. I also observe that inserting the `savefig` command actually makes the right hand side `ylabel` to show up:

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

When I double click on it in the Windows File Explorer and open it in MS Edge, the right hand side “ylabel” seems to be missing. However, when I open the file using free drawing tool Inkscape, here is what I get:

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

So the right hand side “ylabel” is, in fact ,in the figure.

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [April 24, 2020, 3:37pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/7 "2020-04-24T15:37:46Z")

</div>

Hi. Thanks for your very useful message.  
I reproduced this result and found that even if I put a “gui()” after the last drawing function, which will not be excuted in jupyter notebook, the right axis will show up.

```julia
a = 1:10
b = rand(10)
plot(a,b, label = "randData", ylabel = "Rand data",color = :red, legend = :topleft, grid = :off)
p = twinx()
plot!(p,a,log.(a), label = L"log(x)", legend = :topright, ylabel = "The right Y label",
    xlabel = "numbers", box = :on, grid = :off)
#savefig("test")
gui()

```

I am not santisfied with it. Do you know what is behind this strange behavior?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/0/900396def2daae378ad3318a9fce864f5fd02698.png)

---

<div class="post-metadata">

**Author:** ![Piotr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piotr/32/24541_2.png) [@Piotr](https://discourse.julialang.org/u/Piotr)\
**Post date:** [February 27, 2022, 8:11pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/8 "2022-02-27T20:11:56Z")

</div>

I still have the exactly same problem when I run the codes from [this sample](https://goropikari.github.io/PlotsGallery.jl/src/two_y_axis.html) to generate a plot with two y axes or the code of @Fred_He with both Atom and VSC on Julia is 1.6.5. Actually, with @Fred_He codes, I have to remove L from in front of “log(x)” to be able to run the code, for otherwise I get:

> LoadError: LoadError: UndefVarError: @L\_str not defined  
> in expression starting at …

Any ideas why this right scale is still cut in half and y-label missing?

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [February 27, 2022, 9:14pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/9 "2022-02-27T21:14:50Z")

</div>

I figured out it’s all about the width of the margin.  
if you got something cut, add keywords of `xx_margin` into the call of plot, for example, if you don’t see your right ylabel, plot the figure with:

```julia
plot(x, y1, right_margin=5Plots.mm)
plot!(twinx(), x, y2)

```

Remember you should put right\_margin keyword in the first call of plot, not during use the twinx(). if you still don’t see it, use a larger value for the right\_magin, e.g. `, right_margin=10Plots.mm`  
similarly, if you don’t see xlabel, add `bottom_margin`, if you don’t see left ylabel, add `left_margin`

hope this can help.

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [February 27, 2022, 9:25pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/10 "2022-02-27T21:25:52Z")

</div>

In addition, the errot

> UndefVarError: @L\_str not defined

can be resolved by `using LaTeXStrings`  
or simply `label="\$log(x)\$"`

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [November 1, 2022, 8:49pm UTC](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750/11 "2022-11-01T20:49:43Z")

</div>

Fixed by [fix inset padding by t-bltg · Pull Request #4488 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/pull/4488).
