# (Plots.jl) linked (aligned) x-axis figures with outer legends

**URL:** <https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523>\
**Category:** New to Julia\
**Tags:** plotting, plots\
**Created:** [June 25, 2021, 2:27am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523 "2021-06-25T02:27:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 25, 2021, 2:27am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/1 "2021-06-25T02:27:10Z")

</div>

I’m trying to align x-axis of subfigures where the subfigures have legends at e.g. :outerlefttop.

For example, inner legends imply aligned x-axis subfigures.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/9/a916f2760f9b541d3370ccbdbe0acc295186c696.jpeg)

However, outer legends do not.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/3/e348812bc2a35ecab1eb8c23f3e802dc5b9f2223.jpeg)

---

<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:** [June 25, 2021, 5:57am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/2 "2021-06-25T05:57:20Z")

</div>

See also same Plots.jl Github issue [here](https://github.com/JuliaPlots/Plots.jl/pull/2784).

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 25, 2021, 6:14am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/3 "2021-06-25T06:14:18Z")

</div>

Umm… frankly speaking, I didn’t understand what you meant as the linked issue seems not about this question. Isn’t it?

EDIT: My question may be poor 🙂 What I want is to make all x axes’ length equal for subfigures when legends are placed out of figures.

---

<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:** [June 25, 2021, 6:32am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/4 "2021-06-25T06:32:29Z")

</div>

It looks the same as one of the issues mentioned regarding outer legends and linked vertical subplots with axis not aligned:  
 ![Vertical_subplots_outerlegend](https://global.discourse-cdn.com/julialang/original/3X/6/9/69591d7f782e680c51f428ab5d0eb204f9ee7ab6.png)

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 25, 2021, 6:33am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/5 "2021-06-25T06:33:10Z")

</div>

Ah, it’s not a remedy but an already-reported issue. Got it.

---

<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:** [June 25, 2021, 6:43am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/6 "2021-06-25T06:43:44Z")

</div>

Sorry, my bad. The issue reported does not seem to be the same.

---

<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:** [June 25, 2021, 7:09am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/7 "2021-06-25T07:09:04Z")

</div>

FWIW, for the github example linked above, the following workaround produces an equivalent plot, with “outer” legends and axis aligned:

```julia

using Printf, StatsPlots; gr()

d16mer = Dict("Making BioSeq"=> 0.719,
    "Iterating kmers"=> 0.104,
    "Checking sym kmer"=> 0.063,
    "Reading line"=> 0.06,
    "Creating kmer iterator"=> 0.037
    )

dcpg = Dict("Making BioSeq"=> 0.695,
    "Reading line"=> 0.06,
    "GpC detection"=> 0.046,
    "Length of seq"=> 0.032,
    "Indexing seq"=> 0.038
    )

function bp(dict; kw...)
    dict["Misc"] = 1 .- sum(values(dict))
    vals = reverse(collect(values(dict)))'
    labs = permutedims(reverse(collect(keys(dict))))
    groupedbar(vals, label = labs, bar_position = :stack, orientation=:horizontal,yticks = []; kw...)
end

x = LinRange(0,1,5)
xstr = [@sprintf("%.2f", xi) for xi in x]
a = bp(d16mer)
plot!(x, 0*x .+ ylims(a)[1], lc=:black,label=false,widen=false)
xticks!(x, xstr)
b = bp(dcpg)
plot!(x, 0*x .+ ylims(b)[1], lc=:black,label=false,widen=false)
xticks!(x, xstr)
plot(a,b, link=:x,layout=(2,1),xlims=(0,1.7),legend=:topright,showaxis=false)

```

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

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 25, 2021, 7:20am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/8 "2021-06-25T07:20:51Z")

</div>

How did you do that?!

Is it related to the keyword argument `widen=false`?

---

<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:** [June 25, 2021, 7:32am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/9 "2021-06-25T07:32:05Z")

</div>

It is just a inner that looks like an outer, by tricking the axes’ display. 🙂

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [June 25, 2021, 7:33am UTC](https://discourse.julialang.org/t/plots-jl-linked-aligned-x-axis-figures-with-outer-legends/63523/10 "2021-06-25T07:33:27Z")

</div>

Oh, got it. Nice!
