# Makie: multiple y-axes align yticks

**URL:** <https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471>\
**Category:** Graphs\
**Tags:** makie, glmakie\
**Created:** [July 1, 2024, 12:55pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471 "2024-07-01T12:55:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasj](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@thomasj](https://discourse.julialang.org/u/thomasj)\
**Post date:** [July 1, 2024, 12:55pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471/1 "2024-07-01T12:55:55Z")

</div>

I’m trying to make plots with multiple y-axes in Makie.  
I would like that the ticks of all y-axes are aligned with respect to the others y-axis.

```Julia
using GLMakie
using LaTeXStrings
using Statistics

t = [0; 1; 2; 3; 4; 5]
y1 = [9; 7; 5; 1; 3; 5]
y2 = [12; 25; 23; 16; 34; 19]
y3 = [0; -1; -2; -3; -4; -5]

yticks1 = LinRange(minimum(y1) * 0.9, maximum(y1) * 1.1, 5)
yticks2 = LinRange(minimum(y2) * 0.9, maximum(y2) * 1.1, 5)
yticks3 = LinRange(minimum(y3) * 0.9, maximum(y3) * 1.1, 5)

fig = Figure(size = (1600, 900))

ax1 = Axis(fig[1, 1],
           title = "Test 1",
           xlabel = L"Time $t$ in $[s]$",
           ylabel = L"Values $V$ in $[unit]$",
           yticklabelcolor = :blue,
           ylabelcolor = :blue,
           xminorgridvisible = true,
           yminorgridvisible = true,
           xminorticks = IntervalsBetween(4),
           yminorticks = IntervalsBetween(4),
           yticks = yticks1
          )

ax2 = Axis(fig[1, 1],
           ylabel = L"Values $V$ in $[unit]$",
           yticklabelcolor = :red,
           yaxisposition = :right,
           ylabelcolor = :red,
           yticks = yticks2
          )

ax3 = Axis(fig[1, 1],
           ylabel = L"Values $V$ in $[unit]$",
           yticklabelcolor = :green,
           yaxisposition = :right,
           ylabelcolor = :green,
           yticklabelpad = 64.0,
           yticks = yticks3
          )

hidespines!(ax2)
hidespines!(ax3)
hidexdecorations!(ax2)
hidexdecorations!(ax3)

lines!(ax1,
       t,
       y1,
       label = L"line $y1$",
       color = :blue
      )

lines!(ax2,
      t,
      y2,
      label = L"line $y2$",
      color = :red
     )

lines!(ax3,
       t,
       y3,
       label = L"line $y3$",
       color = :green
      )

display(fig)

```

In the example above I tried to set the y-ticks myself on all three y-axes, without success.  
As can be seen in the plot below:

 ![test1](https://global.discourse-cdn.com/julialang/original/3X/c/5/c51477180804750d9cd466bd98d140175594b577.png)

It is also odd that despite specifying 5 y-ticks for all y-axes, the first one displays only 4 ticks, the second one three ticks, and only the last one displays the required 5 ticks.

Does someone knows what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![thomasj](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@thomasj](https://discourse.julialang.org/u/thomasj)\
**Post date:** [July 1, 2024, 1:01pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471/2 "2024-07-01T13:01:09Z")

</div>

Apparently setting the ylimit!() for all axes solve the issue.

Edit: Here’s an example if someone has the same issue:

```Julia
using Makie
using LaTeXStrings
using Statistics

t = [0; 1; 2; 3; 4; 5]
y1 = [9; 7; 5; 1; 3; 5]
y2 = [12; 25; 23; 16; 34; 19]
y3 = [0; -1; -2; -3; -4; -5]

rng1 = abs(maximum(y1) - minimum(y1)) * 0.05
rng2 = abs(maximum(y2) - minimum(y2)) * 0.05
rng3 = abs(maximum(y3) - minimum(y3)) * 0.05

yticks1 = LinRange(floor(minimum(y1) - rng1), ceil(maximum(y1) + rng1), 5)
yticks2 = LinRange(floor(minimum(y2) - rng2), ceil(maximum(y2) + rng2), 5)
yticks3 = LinRange(floor(minimum(y3) - rng3), ceil(maximum(y3) + rng3), 5)

fig = Figure(size = (1600, 900))

ax1 = Axis(fig[1, 1],
           title = "Test 1",
           xlabel = L"Time $t$ in $[s]$",
           ylabel = L"Values $V$ in $[unit]$",
           yticklabelcolor = :blue,
           ylabelcolor = :blue,
           xminorgridvisible = true,
           yminorgridvisible = true,
           xminorticks = IntervalsBetween(4),
           yminorticks = IntervalsBetween(4),
           yticks = yticks1
          )

ax2 = Axis(fig[1, 1],
           ylabel = L"Values $V$ in $[unit]$",
           yticklabelcolor = :red,
           yaxisposition = :right,
           ylabelcolor = :red,
           yticks = yticks2
          )

ax3 = Axis(fig[1, 1],
           ylabel = L"Values $V$ in $[unit]$",
           yticklabelcolor = :green,
           yaxisposition = :right,
           ylabelcolor = :green,
           yticklabelpad = 64.0,
           yticks = yticks3
          )

ylims!(ax1, floor(minimum(y1) - rng1), ceil(maximum(y1) + rng1))
ylims!(ax2, floor(minimum(y2) - rng2), ceil(maximum(y2) + rng2))
ylims!(ax3, floor(minimum(y3) - rng3), ceil(maximum(y3) + rng3))
hidespines!(ax2)
hidespines!(ax3)
hidexdecorations!(ax2)
hidexdecorations!(ax3)

lines!(ax1,
       t,
       y1,
       label = L"line $y1$",
       color = :blue
      )

lines!(ax2,
      t,
      y2,
      label = L"line $y2$",
      color = :red
     )

lines!(ax3,
       t,
       y3,
       label = L"line $y3$",
       color = :green
      )

fig
save("test2.png", fig)

```

 ![test3](https://global.discourse-cdn.com/julialang/original/3X/6/a/6ac2ee9fdd6ed350d05e8e48fac2f849abb7dc4f.png)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [July 1, 2024, 1:12pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471/3 "2024-07-01T13:12:10Z")

</div>

I think you’re looking for `linkyaxes!(ax1, ax2)`?

---

<div class="post-metadata">

**Author:** ![thomasj](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@thomasj](https://discourse.julialang.org/u/thomasj)\
**Post date:** [July 1, 2024, 1:14pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471/4 "2024-07-01T13:14:40Z")

</div>

I tried without success.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [July 1, 2024, 3:34pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471/5 "2024-07-01T15:34:28Z")

</div>

I recently ran into the same issue where I have two axes with a different scales, and came to pretty much the same solution of manually having to force the major ticks to be coaligned by specifying the limits and ticks.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 1, 2024, 4:27pm UTC](https://discourse.julialang.org/t/makie-multiple-y-axes-align-yticks/116471/6 "2024-07-01T16:27:51Z")

</div>

Linkaxes is only for exactly the same limits. The solution with manual ticks and manual limits is the right one. I don’t think you can have it simpler because if you don’t specify limits, it’s unlikely any ticks will be in the same locations by chance.
