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

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:

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

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!

One way - no bells & whistles:

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)

1 Like

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’écran 2022-03-30 à 10.20.50

But that would do for now!

Everything is possible:

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)
2 Likes

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

1 Like

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 :+1:

Thank you for your help!