Plots.jl: labels overlap if using LaTeXStrings


There seems to exist an od behavior of Plots when we use LaTeXStrings to format labels: they overlap. This problem is new because I am revisiting some teaching material, and it did not happen before.

I am on a Windows 10 machine, Julia 1.6.3, using Plots.jl v1.22.5, GR v0.61.0, LaTeXStrings v1.2.1. Please find an example below (it works the same way, even in simple plots with just two labels).

Help would be very much appreciated. I came across a turnaround to this problem here. Unfortunately, it is too elaborate to pass on to students, who have to produce a lot of plots. Thanks.

The safest solution if using LaTeXStrings is to switch the backend to PGFPlotsX. But I reported an issue here: overlap of labels with LaTeXStrings · Issue #434 · jheinen/GR.jl · GitHub

@VivMendes, if the linked PR hasn’t been implemented yet, please find variations on the same theme below, with a different type of burger sandwich:

using LaTeXStrings, Plots; gr()
f(x, n) = x^n
x = 0:0.01:1;
clrs = [:red :blue :green  :cyan :magenta]
fig = plot(xlims=(0, 1), ylims=(0, 1), xlabel="x", ylabel="y", legend=:outerright)
plot!(x, NaN*x, lc=:white, label=" ")
for n in 1:5
    plot!(x, f.(x, n), lc=clrs[n], label=L"y_{%$n}(x) = x^{%$n}")
    plot!(x, NaN*x, lc=:white, label=" ")


Of course, you may define a function to automate the production of burgers.


@lmiq, that’s an option, and if the problem is not solved by the Plots team, possibly a good one. But I have a lot of plots and the arguments of Plots and PGFPlotsX are sometimes quite different. There was a similar problem in the past and it was solved (it was not about overlapping; symbols would not display correctly). Confusing that this LaTeXStrings problem keeps coming up. Thanks for the suggestion.

Yeah, the rendering of latex within GR is a work in progress.

@rafael.guerra, you are a magician! That definitely solves the problem. But I need to keep the plotting issues as simple as possible. This is for master students in economics that are learning some programming techniques for the first time … in an economics course. If I do not keep it simple, they will get frustrated … and my Matlab colleagues will tell me “I told you so”. Thanks anyway.

1 Like

Uhm… IMHO, learning LaTeX syntax is much more complex than cooking the simple burger, but you know your students better.

Btw, a good lesson from Confucius: Better a diamond with a flaw than a pebble without.

NB: the diamonds here are Julia and Plots.jl


I got this answer in the issue:

You can use legend_hfactor > 1 to fine tune the spacing

Didn’t try it.

1 Like

This works:

julia> plot(rand(10),label=L"2\pi")

julia> plot!(rand(10),label=L"2\pi",legend_hfactor=1.5,extra_kwargs=:subplot)

Yet I agree that it is not very friendly to pass to students.

@lmiq, thanks. The extra_kwargs=Dict(:subplot=>Dict(:legend_hfactor=>1.25)) indeed does the trick. That is good news.

The computer in my office had an older version of Plots and GR, and the plots produced last year look perfect (see an example below). Don’t know why upgrades keep coming up with new bugs as far as LaTeXStrings is concerned.

@rafael.guerra, yes, you are right: Julia and Plots are two diamonds. And thank you very much for your speedy and immaculate solutions. They always work. Thanks for your kind help.