# In Plots.jl, can I have a single x axis for a grid of plots?

**URL:** <https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764>\
**Category:** New to Julia\
**Tags:** plots\
**Created:** [August 11, 2020, 10:00pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764 "2020-08-11T22:00:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 11, 2020, 10:00pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/1 "2020-08-11T22:00:06Z")

</div>

I have an array of plots (49 of them so it’s a fairly large figure)

```julia
plts = Plots.Plot[]
for i = 1:49 
   p = plot(...)
   push!(plts, p)
end
plot(plts..., layout = 49, size=(2000, 2000), link=:x, margin=-1mm)

```

I have two questions here

1. Each plot gets its own x axis and y axis. Since the x-axis is the same for all plots, I’d like a single axis at the bottom of each column. I thought the `link = :x` would work, but I don’t know if I am using it correctly.

2. The y-tick-labels have a large gap between the y axis. (note, I am not talking about the axis label… but the actualy axis-tick values). Is there a way to reduce this margin?

---

<div class="post-metadata">

**Author:** ![onButtonUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/onbuttonup/32/3098_2.png) [@onButtonUp](https://discourse.julialang.org/u/onButtonUp)\
**Post date:** [August 13, 2020, 3:33am UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/2 "2020-08-13T03:33:53Z")

</div>

> [@affans](#):
>
> I thought the `link = :x` would work

Maybe Plots.link\_axes!()

Below outputs a set of 49 plots.

```julia
using Plots

C = [0.0005, 0.01, -0.003, 0.00029]
inS, outS = 52, 49
dampen = 0.95

X=zeros(Float64, inS)

# Create synthetic input set.
for i ∈ 1:inS
    X[i] = 0.125i
end

Y = zeros(Float64, (inS, outS))

# Create synthetic output set.
for k ∈ 1:length(Y[1,:]) # For each output Y[n].
    for j ∈ 1:length(X) # For each input X[n].
        Y[j, k]=(C[4]*(X[j]^3)+C[3]*(X[j]^2)+C[2]*X[j]+C[1]) * dampen
    end
    global dampen *= 0.95
end

plot(size=(1000, 1000))

for i ∈ 1:length(Y[1,:])
    display(plot!(X,Y[:,i]))
end

```

 ![009](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d324cce176361cdcd9ea12bae9744365613714d.png)

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 13, 2020, 7:14pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/3 "2020-08-13T19:14:06Z")

</div>

Actually, I meant something like this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/840824c0ffecab67e2e75b2eb8265a979f01abc2.png)

---

<div class="post-metadata">

**Author:** ![fabgrei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabgrei/32/4214_2.png) [@fabgrei](https://discourse.julialang.org/u/fabgrei)\
**Post date:** [August 13, 2020, 8:42pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/4 "2020-08-13T20:42:47Z")

</div>

If you want to move beyond Plots.jl, you can look at [https://github.com/JuliaPlots/AlgebraOfGraphics.jl](https://github.com/JuliaPlots/AlgebraOfGraphics.jl). It builds a layer on top of AbstractPlotting/Makie. It can do this.

---

<div class="post-metadata">

**Author:** ![onButtonUp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/onbuttonup/32/3098_2.png) [@onButtonUp](https://discourse.julialang.org/u/onButtonUp)\
**Post date:** [August 18, 2020, 4:58am UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/5 "2020-08-18T04:58:53Z")

</div>

> [@affans](#):
>
> Actually, I meant something like this:
> 
> ![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/840824c0ffecab67e2e75b2eb8265a979f01abc2.png)

Queryverse is a nice option.

```julia
using Queryverse
using RDatasets: dataset

dataset("ggplot2", "mpg") |>
    @vlplot(:point, x=:Displ, y=:Hwy, column="Class:n", row="Cyl:n")

```

 ![Hwy MPG by Displacement](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4c9a9ebc64cbb6af1fc7efcd383ae99a776ca83.png)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 24, 2020, 3:40pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/6 "2020-08-24T15:40:19Z")

</div>

Maybe this is enough, inside an appropriate loop, using Plots:

```julia
julia> x = rand(10) ; y = rand(10) ;

julia> plot(layout=(2,2),framestyle=:box)

julia> plot!(x,y,xformatter=_->"",subplot=1)

julia> plot!(x,y,xformatter=_->"",yformatter=_->"",subplot=2)

julia> plot!(x,y,subplot=3)

julia> plot!(x,y,yformatter=_->"",subplot=4)

julia> plot!(xlim=[0,1],ylim=[0,1])

```

![image](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a96e7121c7148a44ae9ac7fc27b2b7fadc73bff.png)

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [August 24, 2020, 4:06pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/7 "2020-08-24T16:06:27Z")

</div>

Another option - using MakieLayout, see an example with common y-axis from here: [MakieLayout.jl Tutorial · MakieLayout.jl](https://jkrumbiegel.github.io/MakieLayout.jl/stable/)

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [August 24, 2020, 4:15pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/8 "2020-08-24T16:15:30Z")

</div>

it works for me with `link=:all`.

---

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [February 4, 2022, 4:37pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/9 "2022-02-04T16:37:52Z")

</div>

> [@tobydriscoll](#):
>
> it works for me with `link=:all` .

This is interesting: it works for me with `link=:all` . I am wondering, if :  
Is it also possible to link two subplots like subplot 1 and 2 in a plot(layout(2,2))?

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [February 4, 2022, 9:42pm UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/10 "2022-02-04T21:42:56Z")

</div>

> [@ga72kud](#):
>
> Is it also possible to link two subplots like subplot 1 and 2 in a plot(layout(2,2))?

This may not be the elegant way, but it is possible.

```julia
x = range(0,1,101)
p1 = plot(x,[sin.(x) exp.(x)],layout=(2,1),link=:all,label=nothing);
p2 = plot(x,[cos.(x) tan.(1.5x)],layout=(2,1),label=nothing);
plot(p1,p2,layout=(1,2))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 5, 2022, 8:03am UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/11 "2022-02-05T08:03:33Z")

</div>

If organizing the 2x2 display by columns (as in @tobydriscoll’s nice example) one can link `:all` or `:x`, but not the `:y` axes

On the other hand, if organizing it by rows, one can link `:all` or `:y`, but not the :x axes

This seems to be consistent with the [link keyword description](https://docs.juliaplots.org/latest/generated/attributes_plot/) in the Plots attributes section.

---

<div class="post-metadata">

**Author:** ![ga72kud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ga72kud/32/32180_2.png) [@ga72kud](https://discourse.julialang.org/u/ga72kud)\
**Post date:** [February 7, 2022, 8:46am UTC](https://discourse.julialang.org/t/in-plots-jl-can-i-have-a-single-x-axis-for-a-grid-of-plots/44764/12 "2022-02-07T08:46:34Z")

</div>

I have found a workaround, which works for me, but I think it is not elegant:

```julia
plot(layout=(2,2), link=:all)

```

Which links all axes. I have to zoom in subplot 3 and subplot 3 with the xlims, ylims option. I was hoping that one could doing something like this:

```julia
plot(layout=(2,2), link=<<only a subset of the subplots>>)

```

I guess that this feature does not exist (yet)
