# Avoid repeating tick labels with subplots in Plots.jl

**URL:** <https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [November 16, 2016, 12:14pm UTC](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351 "2016-11-16T12:14:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [November 16, 2016, 12:14pm UTC](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351/1 "2016-11-16T12:14:32Z")

</div>

I’m making grids of subplots in Plots.jl by composing several plots together:

```julia
using Plots
default(legend=false, tickfont=font(12))
x = -6:0.1:6
plots = map(f -> plot(x, f(x), title=string(f), tickfont=font(12)), [sin, cos, sinh, cosh])
plotgrid = plot(plots..., layout=grid(2,2), link=:both)

```

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

Now I’d like to remove unnecessary tick labels: because both axes are linked, there’s some unnecessary duplication.

```julia
let none = Int[]
  xticks!(plots[1], none)
  xticks!(plots[2], none)
  yticks!(plots[2], none)
  yticks!(plots[4], none)
  plotgrid
end

```

![](https://global.discourse-cdn.com/julialang/original/1X/98aa9ad4112cd0a98a96199f301c9607edb8c7ec.png)

But this has the side effect of 1) misaligning the subplots and 2) removing the grid lines.

Has anyone had success doing this sort of thing?

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [November 16, 2016, 2:25pm UTC](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351/2 "2016-11-16T14:25:47Z")

</div>

On master this will work:

```julia
julia> using Plots; funcs=[sin,cos,sinh,cosh];

julia> p = plot(funcs,-6,6,link=:both,layout=4,title=reshape(map(string,funcs),1,4),leg=false)

julia> for i=1:2; plot!(p[i],xformatter=_->""); plot!(p[2i],yformatter=_->""); end; p

```

![](https://global.discourse-cdn.com/julialang/original/3X/8/2/8271ab635626fefadb18c96eb60571f866c97484.png)

I set the formatter to always return an empty string, so that the ticks will still be there (so the grid lines will be drawn). If you don’t care about the grid, you can also set `xticks=nothing`.

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [November 16, 2016, 2:38pm UTC](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351/3 "2016-11-16T14:38:58Z")

</div>

That’s perfect - I was using dev at the time, which had an issue that I see you have already fixed. Didn’t know about xformatter and yformatter but they will be very useful. Amazing work as usual. Thanks!

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [November 16, 2016, 4:23pm UTC](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351/4 "2016-11-16T16:23:56Z")

</div>

I’ve noticed that the misalignment persists if you build a plot from pre-existing ones rather than doing it all at once.

Your method:

```julia
julia> using Plots; funcs=[sin, cos, sinh, cosh];

julia> p = plot(funcs, -6, 6, layout=4, link=:both)

julia> for i=1:2; plot!(p[i],xformatter=_->""); plot!(p[2i],yformatter=_->""); end; p

```

![](https://global.discourse-cdn.com/julialang/original/3X/4/7/4756e912e4671dae5c81897d47c2f986fddd0967.png)

Making the plots individually first:

```julia
julia> using Plots; funcs=[sin, cos, sinh, cosh];

julia> plots = map(f -> plot(f, -6, 6), funcs); p = plot(plots..., layout=4, link=:both)

julia> for i=1:2; plot!(p[i],xformatter=_->""); plot!(p[2i],yformatter=_->""); end; p

```

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

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [November 16, 2016, 4:49pm UTC](https://discourse.julialang.org/t/avoid-repeating-tick-labels-with-subplots-in-plots-jl/351/5 "2016-11-16T16:49:28Z")

</div>

The reason for this is subtle, and hard to fix generically. When you build a plot from existing subplots, there is an extra layer of layout. Specifically, there’s a 2x2 grid layout where each cell contains a 1x1 grid layout. I guess the alignment logic isn’t passing through multiple recursive layers in the way you would hope.

Ideally the fix would come from within Plots, but since my time is limited, you’ll have to fix it yourself. There’s another way to do this if you want to define each plot individually:

```julia
p = plot(layout=4, link=:both, leg=false)
for (i,f) in enumerate(funcs)
    plot!(p[i], f, -6, 6, title=string(f))
end
p

```

This builds the layout but doesn’t add any series initially. Then you can add to a specific subplot by indexing into the Plot object. Note: you could also do `p[row,column]` in this case to get a specific subplot.
