# Wrapping label text in Plots.jl

**URL:** <https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124>\
**Category:** Visualization\
**Tags:** question, plotting, plots, pgfplotsx\
**Created:** [February 9, 2022, 10:22pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124 "2022-02-09T22:22:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 9, 2022, 10:22pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/1 "2022-02-09T22:22:31Z")

</div>

I have a plot of only two lines with long legend labels. The plot doesn’t have enough horizontal space unless the labels are wrapped. How do I do that?

I’m using `pgfplotsx()` backend in `Plots.jl` for LaTeX-style plots, so I would prefer suggestions within this backend.

---

<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 9, 2022, 11:00pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/2 "2022-02-09T23:00:47Z")

</div>

Try this:

```julia
using Plots; pgfplotsx()
default(legend_font_halign=:left)

x = 1:10; y1, y2 = sin.(x)./x, cos.(x)./x
label1a = "this is a very long pgfplotsx() label, "^2
label1b = "split into a new line"
label2a = "here is another quite long label, "^2
label2b = "split into a new line"
plot(x, y1, label=label1a, legend=:topright, fg_legend=nothing);
plot!([x[1]], [y1[1]], lc=:white, label=label1b);
plot!(x, y2, label=label2a);
plot!([x[1]], [y1[1]], lc=:white, label=label2b)

```

 ![Plots_pgfplots_wrap_long_legends](https://global.discourse-cdn.com/julialang/original/3X/c/0/c07a45c50f1f539dbee7310b2e71ba66ec566f17.png)

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 10, 2022, 3:28pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/3 "2022-02-10T15:28:13Z")

</div>

This is so smart. Thank you!

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 10, 2022, 3:48pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/4 "2022-02-10T15:48:19Z")

</div>

Two points that the solution may be refined:

- The legend line is only aligned with the first label. It would be nicer to align it between the first two labels.
- Inserting two phantom series breaks auto coloring.

---

<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 10, 2022, 3:54pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/5 "2022-02-10T15:54:58Z")

</div>

Try `legend=:top` instead of `:topright`

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 10, 2022, 3:56pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/6 "2022-02-10T15:56:33Z")

</div>

it’s still aligned to the first row.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/a/3a41bd184ee0b31c1e6a8abdd04def4330eb77b7.png)

---

<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 10, 2022, 4:00pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/7 "2022-02-10T16:00:40Z")

</div>

Try: `default(legend_font_halign=:center)` to align all with your prefered default, or you can add the plot keyword, on case by case basis:  
`legend_font_halign=:left`  
`legend_font_halign=:center`

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 10, 2022, 5:13pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/8 "2022-02-10T17:13:47Z")

</div>

Does halign affect vertical placement?

---

<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 10, 2022, 5:16pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/9 "2022-02-10T17:16:22Z")

</div>

Would it be possible to see an example of the desired result?  
As far as automatic coloring is concerned, that can be managed.

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 10, 2022, 5:26pm UTC](https://discourse.julialang.org/t/wrapping-label-text-in-plots-jl/76124/10 "2022-02-10T17:26:12Z")

</div>

![149b1eb6731b8295130e79cc7527fdc24d5f111d_2_517x321](https://global.discourse-cdn.com/julialang/original/3X/7/9/79b3e784a588294a80fbbcda185b95d47d078ba8.png)
