Plot with three y-axes (, so one y-axis has to be double)

Hello,

I need to put on the same graph three lines with different y-axis). I know how to do it with two y-axises (one is right and the other is left).
How to do it with 3 lines, so the left y-axis has to be double.

thank you in advance.
Best

I think the best advice I can give is “don’t”, two axes is already slightly less than one too many.

That said, have you considered

plot(...)
twinx(...)
twinx(...; ymirror=true)

?

2 Likes

thank you,
But here, 2dn and 3rd y-axises overlapped, unfortunately, it’s not doubled

This is an example

Okay, there is probably no simpler alternative than using insets with manual bboxes for that. And the reason is most people would consider that poor visualization. Compared to

plot(
plot(...),
plot(...),
plot(...);
layout=(3,1)
)

the triple-axis plot is just harder to read.

But if you’re using it internally where you can learn to read it once and will then use the same layout many times I guess there is no issue with it if you get it to work.

Have you tried using PyPlot.jl to implement Multiple Yaxis With Spines ?

In Makie you can use the ytickalign keyword for the Axis to move the position of the yticks.

using GLMakie

a = sin.(1:0.01:6); b = sin.(1:0.01:6) .+ randn.();

c = cos.(1:0.01:6) .+ 20

fig = Figure(resolution=(1200,700))
ax1 = fig[1,1] = Axis(fig, ytickalign=-5, yticklabelcolor=:blue)
ax2 = fig[1,1] = Axis(fig, yaxisposition=:right)
ax3 = fig[1,1] = Axis(fig, ytickalign=0, yticklabelcolor=:red)

plot!(ax1, a, color=:blue)
plot!(ax2, b)
plot!(ax3, c, color=:red)

hidexdecorations!(ax1)
hidexdecorations!(ax2)
linkxaxes!(ax1,ax2)
linkxaxes!(ax1,ax3)

This would produce this plot
image

3 Likes

thanks

thank you

1 Like

I have the same problem but unfortunately GLMakie did not work for me. The data in addition to the Jupyter Notebook file can be found here: MPCDF DataShare

I am doing the following:

using Plots
using CSV
using DataFrames
using Colors

df_1 = DataFrame(CSV.File("Energy.csv"))
df_2 = DataFrame(CSV.File("Population.csv"))
df_3 = DataFrame(CSV.File("Ppm.csv"))

a=18
plot(df_2.Year,df_2[:,4],
    xlims=(1800,2021),
    label = "Population", 
    ylabel = "World Population",
    color=RGB(0.337, 0.631, 0.749),
    linewidth=3, 
    legend = :topleft, 
    grid = :off,
    size = (1600,800),
    left_margin = 10Plots.mm, 
    right_margin = 50Plots.mm, 
    xtickfontsize=a,
    ytickfontsize=a,
    xguidefontsize=a,
    yguidefontsize=a,
    legendfontsize=a)
p = twinx()
plot(p,df_3.Year,df_3[:,4],
    xlims=(1800,2021),
    label = "Global CO₂ atmospheric concentration", 
    ylabel = "Global CO₂ concentration in ppm",
    color =RGB(0.439, 0.368, 0.470),
    linewidth=3, 
    legend = :topright, 
    grid = :off,
    size = (1600, 800),
    left_margin = 10Plots.mm, 
    right_margin = 50Plots.mm, 
    xtickfontsize=a,
    ytickfontsize=a,
    xguidefontsize=a,
    yguidefontsize=a,
    legendfontsize=a)
p = twinx()
plot!(p,df_1.Year,df_1.sum,
    xlims=(1800,2021),
    label = "Energy Consumption", 
    ylabel = "World Primary Energy Consumption",
    color =RGB(0.949, 0.352, 0.219),
    linewidth=3, 
    legend = :left, 
    grid = :off,
    size = (1600, 800),
    left_margin = 10Plots.mm, 
    right_margin = 50Plots.mm, 
    xtickfontsize=a,
    ytickfontsize=a,
    xguidefontsize=a,
    yguidefontsize=a,
    legendfontsize=a)

The result is the following:

What I would like to change:

  • Move the Y-ticks + Y-label of ‘Energy’ to the right
  • Color the Y-ticks + Y-label of each graph
  • Put all legends to the :topleft in decending size order.

Please help me to make the plot pretty again

I came up with a satisfying solution myself, after finally finding a solution to move the y-axis label here In Plots.jl, how does one control the space between ylabel and yaxis? - #15 by Andre_Mello. The only thing which I could not solve are the position of the legends. Here is the picture:

And here the corresponding code:

a=18
plot(df_2.Year,df_2[:,4],
    xlims=(1800,2021),
    label = "Population", 
    ylabel = "World Population",
    color=RGB(0.337, 0.631, 0.749),
    linewidth=3, 
    legend = :topleft, 
    grid = :off,
    size = (1600,800),
    left_margin = 10Plots.mm, 
    right_margin = 50Plots.mm, 
    foreground_color_guide = RGB(0.337, 0.631, 0.749),
    ytickfontcolor = RGB(0.337, 0.631, 0.749),
    xtickfontsize=a,
    ytickfontsize=a,
    xguidefontsize=a,
    yguidefontsize=a,
    legendfontsize=a)
p = twinx()
plot(p,df_3.Year,df_3[:,4],
    xlims=(1800,2021),
    label = "Global CO₂ atmospheric concentration", 
    ylabel = "\n\n\n\n\n"*"Global CO₂ concentration in ppm",
    color =RGB(0.439, 0.368, 0.470),
    linewidth=3, 
    legend = :topright, 
    grid = :off,
    size = (1600, 800),
    left_margin = 10Plots.mm, 
    right_margin = 50Plots.mm, 
    yticks = [280,315,345,380,410],
    foreground_color_guide = RGB(0.439, 0.368, 0.470),
    ytickfontcolor = RGB(0.439, 0.368, 0.470),
    xtickfontsize=a,
    ytickfontsize=a,
    xguidefontsize=a,
    yguidefontsize=a,
    legendfontsize=a)
p = twinx()
plot!(p,df_1.Year,df_1.sum,
    xlims=(1800,2021),
    label = "Energy Consumption", 
    ylabel = "World Primary Energy Consumption",
    color =RGB(0.949, 0.352, 0.219),
    linewidth=3, 
    legend = :left, 
    grid = :off,
    size = (1600, 800),
    left_margin = 10Plots.mm, 
    right_margin = 50Plots.mm, 
    foreground_color_guide = RGB(0.949, 0.352, 0.219),
    ytickfontcolor = RGB(0.949, 0.352, 0.219),
    xtickfontsize=a,
    ytickfontsize=a,
    xguidefontsize=a,
    yguidefontsize=a,
    legendfontsize=a)
2 Likes