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.
Try this:
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)
3 Likes
This is so smart. Thank you!
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.
Try legend=:top
instead of :topright
it’s still aligned to the first row.
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
Does halign affect vertical placement?
Would it be possible to see an example of the desired result?
As far as automatic coloring is concerned, that can be managed.