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

**URL:** https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699
**Category:** General Usage
**Tags:** plotting
**Created:** [April 6, 2021, 4:17pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699 "2021-04-06T16:17:34Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [April 6, 2021, 4:17pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/1 "2021-04-06T16:17:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 6, 2021, 4:26pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/2 "2021-04-06T16:26:21Z")

</div>

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

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

```

?

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [April 6, 2021, 5:19pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/3 "2021-04-06T17:19:48Z")

</div>

> [@gustaphe](#):
>
> `ymirror=true`

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

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [April 6, 2021, 5:45pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/4 "2021-04-06T17:45:17Z")

</div>

This is an example

 ![example](https://global.discourse-cdn.com/julialang/original/3X/8/8/88c05993f48b91edbaa9e3483492b747338a77a0.png)

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 6, 2021, 8:38pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/5 "2021-04-06T20:38:09Z")

</div>

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

```julia
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.

---

<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: [April 6, 2021, 9:15pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/6 "2021-04-06T21:15:45Z")

</div>

Have you tried using [PyPlot.jl](https://github.com/JuliaPy/PyPlot.jl) to implement [Multiple Yaxis With Spines](https://matplotlib.org/stable/gallery/ticks_and_spines/multiple_yaxis_with_spines.html) ?

---

<div class="post-metadata">

### Author: ![Fliks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fliks/32/2494_2.png) [@Fliks](https://discourse.julialang.org/u/Fliks)
#### Post date: [April 7, 2021, 7:15am UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/7 "2021-04-07T07:15:29Z")

</div>

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

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/0/1/01ab0e0296fa82a1ff2d8cefab6fd8fc41f226a9.png)

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [April 7, 2021, 2:29pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/8 "2021-04-07T14:29:58Z")

</div>

thanks

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [April 7, 2021, 2:30pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/9 "2021-04-07T14:30:22Z")

</div>

thank you

---

<div class="post-metadata">

### Author: ![dadaforpeace](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dadaforpeace](https://discourse.julialang.org/u/dadaforpeace)
#### Post date: [November 15, 2021, 4:01pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/10 "2021-11-15T16:01:58Z")

</div>

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: [https://datashare.mpcdf.mpg.de/s/uPOltmpkDjlZl4F](https://datashare.mpcdf.mpg.de/s/uPOltmpkDjlZl4F)

I am doing the following:

```julia
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:

 ![Population-Energy-PPM](https://global.discourse-cdn.com/julialang/original/3X/f/b/fbbce9ec35d1d43b9fea84c4ec4c29f6a7a970b0.png)

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

---

<div class="post-metadata">

### Author: ![dadaforpeace](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dadaforpeace](https://discourse.julialang.org/u/dadaforpeace)
#### Post date: [November 16, 2021, 5:49pm UTC](https://discourse.julialang.org/t/plot-with-three-y-axes-so-one-y-axis-has-to-be-double/58699/11 "2021-11-16T17:49:07Z")

</div>

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](https://discourse.julialang.org/t/in-plots-jl-how-does-one-control-the-space-between-ylabel-and-yaxis/5717/15). The only thing which I could not solve are the position of the legends. Here is the picture:

 ![Population-Energy-PPM](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf3dd9c603407603c32ceaf0ee1108e521d70ba5.png)

And here the corresponding code:

```julia
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)

```
