# Align ticks in subplots

**URL:** https://discourse.julialang.org/t/align-ticks-in-subplots/87919
**Category:** Visualization
**Tags:** makie
**Created:** [September 28, 2022, 11:00am UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919 "2022-09-28T11:00:31Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 11:00am UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/1 "2022-09-28T11:00:31Z")

</div>

I have the following code:

```julia
using CairoMakie

d1 = rand(1:10, 50)
d2 = rand(1:4, 20)
d3 = rand(1:10, 10)

fig=Figure()

ax_1=Axis(fig[1, 1:2])
ax_2=Axis(fig[2, 1])
ax_3=Axis(fig[2, 2])

scatter!(ax_1, 1:50, d1)
scatter!(ax_2, 1:20, d2)
scatter!(ax_3, 1:10, d3)

fig

```

Is there a way I can align the xticks of `ax_1` and `ax_2` such that similar numbers are at the same x position? I guess this is only possible by changing the size of the whole subplot?

---

<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: [September 28, 2022, 11:11am UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/2 "2022-09-28T11:11:24Z")

</div>

> similar numbers are at the same x position

I assume you’re not talking about just `linkxaxes!`?

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 11:27am UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/3 "2022-09-28T11:27:34Z")

</div>

I’ve added `colsize!(fig.layout, 1, Relative(0.45))` and now the 20 is inline in the first and second plot. So, of course I could try to approximately align them with `limits` and `colsize` but I was hoping for something more elegant.  
If I add `linkxaxes!(ax_1, ax_2)` `ax_2` has the same limits as `ax_1`. So no, this isn’t what I’m looking for.

---

<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: [September 28, 2022, 12:28pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/4 "2022-09-28T12:28:22Z")

</div>

use `colsize!(fig.layout, 1, Auto(X))` where X is a factor vs the other Auto columns. If you make the factors mirror the limit ranges, you’ll get what you want I think

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 12:41pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/5 "2022-09-28T12:41:59Z")

</div>

Is this what you mean?

```julia
using CairoMakie

d1 = rand(1:10, 50)
d2 = rand(1:4, 20)
d3 = rand(1:10, 10)

fig = Figure()
ax_1 = Axis(fig[1, 1:2],
    limits = (0, 50, nothing, nothing))
ax_2 = Axis(fig[2, 1],
limits = (0, 20, nothing, nothing))
ax_3 = Axis(fig[2, 2])

scatter!(ax_1, 1:50, d1)
scatter!(ax_2, 1:20, d2)
scatter!(ax_3, 1:10, d3)

colsize!(fig.layout, 1, Auto(2/5))

fig

```

If so, then this doesn’t work.

---

<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: [September 28, 2022, 2:39pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/6 "2022-09-28T14:39:20Z")

</div>

I still don’t know exactly what you want, a visual example would help. Also “this doesn’t work” doesn’t tell me anything I can use 🙂

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 2:54pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/7 "2022-09-28T14:54:22Z")

</div>

Alright, so I made a rough sketch:

 ![plot_28](https://global.discourse-cdn.com/julialang/original/3X/4/8/4867bd7b2beca1b85b0970ff0540b09b3cbb22b7.png)

I want the grid lines between the first plot and the second one perfectly lined up as show with the red lines. So the 20 of the first plot should be aligned to the 20 of the second one, etc.

I hope this clears things up.

---

<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: [September 28, 2022, 2:58pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/8 "2022-09-28T14:58:20Z")

</div>

What about the graph on the lower right then?

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 3:03pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/9 "2022-09-28T15:03:45Z")

</div>

Doesn’t matter… can get squished into a different aspect ratio if that’s needed.

---

<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: [September 28, 2022, 3:04pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/10 "2022-09-28T15:04:52Z")

</div>

No I mean you only care about the ticks of the left one aligning? The right one has a different range anyway

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 3:05pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/11 "2022-09-28T15:05:48Z")

</div>

yes, exactly

---

<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: [September 28, 2022, 3:11pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/12 "2022-09-28T15:11:31Z")

</div>

One option:

```julia
using CairoMakie

d1 = rand(1:10, 50)
d2 = rand(1:4, 20)
d3 = rand(1:10, 10)

fig = Figure()
ax_1 = Axis(fig[1, 1:2],
    limits = (0, 50, nothing, nothing))
ax_2 = Axis(fig[2, 1],
limits = (0, 20, nothing, nothing))
ax_3 = Axis(fig[2, 2])

scatter!(ax_1, 1:50, d1)
scatter!(ax_2, 1:20, d2)
scatter!(ax_3, 1:10, d3)

ax_2.width = ax_1.scene.px_area[].widths[1] / 50 * 20

fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/3/63cdf781a09fb1e77e1adc0089f8b52c3a7eda13.png)

This will not update if you change more stuff after, though.

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 6:16pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/13 "2022-09-28T18:16:34Z")

</div>

That looks great. Thanks @jules .  
What do you mean with that this will not update if I change more stuff after?

---

<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: [September 28, 2022, 6:27pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/14 "2022-09-28T18:27:58Z")

</div>

If the limits or axis widths change then the computed width will not be correct anymore. One could probably set this up via observables but it’s not necessary if you don’t change the figure like that after setting the width.

---

<div class="post-metadata">

### Author: ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)
#### Post date: [September 28, 2022, 6:45pm UTC](https://discourse.julialang.org/t/align-ticks-in-subplots/87919/15 "2022-09-28T18:45:57Z")

</div>

Alright. Thanks!
