# Align axes to each other with Makie.jl

**URL:** https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124
**Category:** Visualization
**Tags:** makie, makielayout
**Created:** [August 22, 2022, 9:48am UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124 "2022-08-22T09:48:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nvhoang.3110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvhoang.3110/32/37168_2.png) [@nvhoang.3110](https://discourse.julialang.org/u/nvhoang.3110)
#### Post date: [August 22, 2022, 9:48am UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/1 "2022-08-22T09:48:58Z")

</div>

Hello everyone. I’m having trouble with aligning axes of two plots with different scales. Below is a reproducible example:

```julia
begin
    fig = Figure(resolution = (1920, 1080));
    update_theme!(fontsize = 24);
    axis = fig[1, 1] = Axis(fig, title = "First plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:20:80));
    axis2 = fig[1, 2] = Axis(fig, title = "Second plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:125:500));
    violin!(axis, rand((0, 1), 500), rand(0:80, 500), color = "#92a8d1");
    violin!(axis2, rand((0, 1), 500), rand(0:500, 500), color = "#b1cbbb");
    linkyaxes!(axis, axis2);
    fig
end

```

The code produces this plot, which you can see the y-axes of two plots are aligned but not in the way that I expected. The first plot is compressed in order to match with the y-axis of the second one.

 ![example](https://global.discourse-cdn.com/julialang/original/3X/a/a/aafa232c063cfccabe6e76acacf2a7827c8c59e7.png)  
So my question is, is there a way in Makie to align the axes with different scales ? Thank you in advance !

---

<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: [August 22, 2022, 9:50am UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/2 "2022-08-22T09:50:52Z")

</div>

That’s exactly what `linkyaxes!(axis, axis2)` does, it gives both axes the same y-range. One is just much larger than the other in your example. What is the behavior you would like instead?

---

<div class="post-metadata">

### Author: ![nvhoang.3110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvhoang.3110/32/37168_2.png) [@nvhoang.3110](https://discourse.julialang.org/u/nvhoang.3110)
#### Post date: [August 22, 2022, 10:02am UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/3 "2022-08-22T10:02:48Z")

</div>

Hi, thanks for your replying. Maybe I’m using the wrong functionality. What I want to see is two plots are drawn on the same grid with, for example, 5 ticks each on the y-axis that’re aligned with each other. I tried to set the number of ticks as well as ticks displayed manually but the result is far from what I want. Do you have any idea how to handle case like this ?

---

<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: [August 22, 2022, 11:29am UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/4 "2022-08-22T11:29:25Z")

</div>

Can you show a visual example from some other plot that looks like what you’re after? I’m a bit confused because you have two distributions with very different ranges, so either each has its own y axis range, and they won’t have equal ticks, or you link the axes and achieve the result from above.

---

<div class="post-metadata">

### Author: ![nvhoang.3110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvhoang.3110/32/37168_2.png) [@nvhoang.3110](https://discourse.julialang.org/u/nvhoang.3110)
#### Post date: [August 22, 2022, 1:53pm UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/5 "2022-08-22T13:53:03Z")

</div>

Hi, below is the example that I’m trying to recreate in Makie. Please note that in the picture both plots are drawn in a same grid which are aligned with each other, despite as you said they came from a different distributions. Also, the ticks are well scattered across the y-axis as well, which is not the case in my plot. I’m not sure whether it’s even possible to create something like that or not.  
 ![Example 2](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d895621f3fc6639ce1c770a429ba16c20e31b4b.png)

---

<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: [August 22, 2022, 5:38pm UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/6 "2022-08-22T17:38:58Z")

</div>

The ticks are not spaced well because your plots are much wider than the domain you’re sampling from. That’s because the kernel density extends farther than the extrema of the data. But you can set `datalimits` for that on the `violin!` call.

Other than that there’s nothing “linked” about this plot, it’s just manually chosen to look nice I assume.

Here’s with `datalimits = extrema`. Much better but you might want to control the limits exactly if you want a perfect match, right now the `extrema` are a little bit random given the input data.

```julia
begin
    fig = Figure();
    axis = fig[1, 1] = Axis(fig, title = "First plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:20:80));
    axis2 = fig[1, 2] = Axis(fig, title = "Second plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:125:500));
    violin!(axis, rand((0, 1), 500), rand(0:80, 500), color = "#92a8d1", datalimits = extrema);
    violin!(axis2, rand((0, 1), 500), rand(0:500, 500), color = "#b1cbbb", datalimits = extrema);
    fig
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c0a18a522562e4c925f33e1089f021109b0a192.png)

---

<div class="post-metadata">

### Author: ![nvhoang.3110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvhoang.3110/32/37168_2.png) [@nvhoang.3110](https://discourse.julialang.org/u/nvhoang.3110)
#### Post date: [August 22, 2022, 6:18pm UTC](https://discourse.julialang.org/t/align-axes-to-each-other-with-makie-jl/86124/7 "2022-08-22T18:18:08Z")

</div>

You’re right, I may have think about how to design my plot again. Thank you so much for your help ❤
