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

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.

1 Like

Could you post a minimum working example as described in Please read: make it easier to help you? It’s very hard to help you without knowing what you’re doing exactly, and what plotting package you’re actually using.

Fair, sorry about that.

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="")

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

Problem still there!

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:

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:

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

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.

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?

1 Like

I still have the exactly same problem when I run the codes from this sample 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?

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:

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.

2 Likes

In addition, the errot

UndefVarError: @L_str not defined

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

1 Like

Fixed by fix inset padding by t-bltg · Pull Request #4488 · JuliaPlots/Plots.jl · GitHub.