Increase space between legend labels in Plots

Is there a way to increase the spacing between the legend labels in the histogram below so that they are legible? Thanks!

using LaTeXStrings

y1 = rand(100)
y2 = rand(100)
histogram(y1, ylims = (0,50), label = L"\left\Vert\beta_{1}^{2}-\beta_{2}^{2}\right\Vert")
histogram!(y2, label = L"\left\Vert\beta_{2}^{2}-\beta_{3}^{2}\right\Vert")

plot

2 Likes

If you use the plotly() backend: latex - Julia plotting issues: label overlap and LaTeXStings expansion - Stack Overflow

I didn’t find a way to do it with the gr() backend. You can control the font with, say, legendfontsize=14.0 and there is an option for labelspacing=0.1 (typical values seem to be small), but I’m not sure what labelspacing actually does: it sounds like it might control the horizontal spacing, but I did not notice an effect on your plot. Hopefully someone from Plots.jl can help you with this. Alternatively, introducing a line skip inside the LaTeX string: I also failed to achieve that with the usual suspects: "\\n", "\\\", "\\vspace{2em}" :flushed:

1 Like

@Dan.phi, with Plots.jl you often need to be creative and it will rarely disappoint.

using LaTeXStrings, Plots; gr()

y1, y2 = rand(100), rand(100)
histogram(y1, ylims = (0,50), label = L"\left\Vert\beta_{1}^{2}-\beta_{2}^{2}\right\Vert")
scatter!([minimum(y1)],[0], label=" ", ms=0, mc=:white, msc=:white)
histogram!(y2, label = L"\left\Vert\beta_{2}^{2}-\beta_{3}^{2}\right\Vert", legendfontsize=10)

NB: nevertheless, your code produces a legend that is perfectly legible in Julia 1.6.1, Win 10, Plots v1.16.3, gr backend

3 Likes

you added an invisible legend item with scatter! in order not to interfere with the other two histogram calls, right? Clever. :+1: This sort of trick is often needed.

I opened an issue because I think it should not be too hard to add a padding option. There is such an option for the plot area itself and the legend is constructed in an analogous way. Unfortunately I don’t know enough about Plots to create a PR, but hopefully it’s a reasonable request.

4 Likes

Huge thanks to @t-bltg, who in response to the issue mentioned above, put together a PR that allows more vertical space inside legends, fixing the OP and allowing more flexibility in general. :pray:

2 Likes

Happy to help :wink:

3 Likes