# Formatting two plots to have the same x axis range

**URL:** <https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882>\
**Category:** Visualization\
**Tags:** question, plotting, cairo\
**Created:** [November 27, 2022, 3:26am UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882 "2022-11-27T03:26:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eduardo-zambrano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardo-zambrano/32/38099_2.png) [@eduardo-zambrano](https://discourse.julialang.org/u/eduardo-zambrano)\
**Post date:** [November 27, 2022, 3:26am UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/1 "2022-11-27T03:26:57Z")

</div>

Hi everyone,

I have what I think is a very basic question about plotting using Cairo Makie. How can I make two plots I’m making have the same x axis range?. Here’s a working prototype that I think illustrates what I need:

```julia
using CairoMakie

f1 = Figure();
Axis(f1[1, 1], title = "Site 1")
d₀ = density!(randn(200))
d₁ = density!(1 .+ randn(200))
f1

f2 = Figure();
Axis(f2[1, 1], title = "Site 2")
d₀ = density!( 1.2 .* randn(200))
d₁ = density!(1.2 .+ 1.2 .* randn(200))
f2

```

If you run this code, you will see that both graphs end up with their own separate x-range. How can I format the figures so that only a certain range, say in my example, the range (-2,2) is shown in both figures?

---

<div class="post-metadata">

**Author:** ![fedoroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedoroff/32/53209_2.png) [@fedoroff](https://discourse.julialang.org/u/fedoroff)\
**Post date:** [November 27, 2022, 6:04am UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/2 "2022-11-27T06:04:54Z")

</div>

[Linking axes](https://docs.makie.org/v0.18.3/examples/blocks/axis/index.html#linking_axes)?

---

<div class="post-metadata">

**Author:** ![eduardo-zambrano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardo-zambrano/32/38099_2.png) [@eduardo-zambrano](https://discourse.julialang.org/u/eduardo-zambrano)\
**Post date:** [November 27, 2022, 2:54pm UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/3 "2022-11-27T14:54:09Z")

</div>

Thank you. That does almost everything I need.

Here’s my updated prototype code.

```julia
using CairoMakie

f = Figure();

ax1 = Axis(f[1, 1], title="Site 1")
ax2 = Axis(f[1, 2],title="Site 2")
linkxaxes!(ax1, ax2)

density!(ax1, randn(200))
density!(ax1, 1 .+ randn(200))

density!(ax2, 1.2 .* randn(200))
density!(ax2, 1.2 .+ 1.2 .* randn(200))

f

```

I still need to control the specific range in the figure to a user generated range, say, (-2,2).

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [November 27, 2022, 3:38pm UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/4 "2022-11-27T15:38:00Z")

</div>

```julia
xlims!.((ax1, ax2), -2, 2)

```

---

<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:** [November 27, 2022, 5:57pm UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/5 "2022-11-27T17:57:05Z")

</div>

To add to this, `linkaxes!` is just for making it convenient to automatically compute and synchronize limits across axes. If you want to set specific limits anyway, you don’t profit from linking and you’re fine using `xlims!`, `ylims!` or `limits!`, respectively. It makes a difference when interacting with figures, but since you use CairoMakie that doesn’t factor into it.

---

<div class="post-metadata">

**Author:** ![eduardo-zambrano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardo-zambrano/32/38099_2.png) [@eduardo-zambrano](https://discourse.julialang.org/u/eduardo-zambrano)\
**Post date:** [November 27, 2022, 6:37pm UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/6 "2022-11-27T18:37:39Z")

</div>

Thank you. That does it!

---

<div class="post-metadata">

**Author:** ![eduardo-zambrano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eduardo-zambrano/32/38099_2.png) [@eduardo-zambrano](https://discourse.julialang.org/u/eduardo-zambrano)\
**Post date:** [November 27, 2022, 6:39pm UTC](https://discourse.julialang.org/t/formatting-two-plots-to-have-the-same-x-axis-range/90882/7 "2022-11-27T18:39:11Z")

</div>

Thank you. You’re right. xlims solves my problem, and it is nevertheless great to know about linkaxes for future reference.
