# Common x-/y-label in Plots.jl

**URL:** <https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [July 21, 2020, 7:03pm UTC](https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445 "2020-07-21T19:03:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antomek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antomek/32/9406_2.png) [@Antomek](https://discourse.julialang.org/u/Antomek)\
**Post date:** [July 21, 2020, 7:03pm UTC](https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445/1 "2020-07-21T19:03:42Z")

</div>

Hello everyone,

I was wondering if there is a way to specify a common x- or y-label easily in Plots.jl when plotting one figure with multiple subplots.  
The following creates subplots with the same axes labels for both figures, but printed for each figure:

```julia
using Plots

p1 = plot(rand(100))
p2 = plot(rand(100))

testplot = plot(p1, p2, link=:both, xlabel="X", ylabel="Y", layout=(2,1))

```

Is there a way to print both the x- and y-label just once?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [July 21, 2020, 9:32pm UTC](https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445/2 "2020-07-21T21:32:40Z")

</div>

Hm. I’ve never seen the `link` keyword for `plot`, so it is not quite clear to me what that means. In a case like this, I’d do the following:

```julia
using Plots

p1 = plot(rand(100))
p2 = plot(rand(100),xlabel="X")

plot(p1,p2,layout=(2,1),ylabel="Y")

```

This gives a shared `xlabel`, but it gives one `ylabel` for each sub plot. I find that natural, but it is perhaps not exactly what you want.

---

<div class="post-metadata">

**Author:** ![Antomek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antomek/32/9406_2.png) [@Antomek](https://discourse.julialang.org/u/Antomek)\
**Post date:** [July 22, 2020, 8:29am UTC](https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445/3 "2020-07-22T08:29:21Z")

</div>

Thanks for the reply!

The `link` keyword determines which axis limits are shared between plots.  
In this case `:both` means both the x- and y-limits are shared.

I do wonder if there is a more general / easier solution though. Seems like something `Plots.jl` should be able to handle…

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 22, 2020, 12:24pm UTC](https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445/4 "2020-07-22T12:24:35Z")

</div>

I would find this useful as well – for grid layouts that is. It would not be hard to write a function that does this, but it would a bit fragile (dependent on internals of Plots.jl). Something like:

```julia
using Plots

function ylabels_from_grid(l :: Plots.GridLayout, yStr)
    n = length(l.grid)
    nCols = size(l.grid, 2)
    yV = fill("", n);
    for j = 1 : n
        if rem(j, nCols) == 1
            yV[j] = yStr;
        end
    end
    return yV    
end

# Make a layout
l = @layout grid(2,2);
yV = ylabels_from_grid(l, "Y label");

# Plot some things
pV = Vector{Any}(undef, 4)
for j = 1 : 4
    pV[j] = plot(1:10, 1:10, xlabel = "$j", ylabel = yV[j])
end
p = plot(pV..., layout = l)

```

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [July 22, 2020, 1:53pm UTC](https://discourse.julialang.org/t/common-x-y-label-in-plots-jl/43445/5 "2020-07-22T13:53:21Z")

</div>

`link` is especially useful if you use the interactive plots of Plotly (by calling `plotly()` before you make your plot) so that when you zoom into one subplot, the other plots stay synchronized.
