Formatting two plots to have the same x axis range

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:

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?

Linking axes?

Thank you. That does almost everything I need.

Here’s my updated prototype code.

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).

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

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.

1 Like

Thank you. That does it!

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