6.4 Using LaTeXStrings.jl
LaTeX support in Makie.jl
is also available via LaTeXStrings.jl
:
using LaTeXStrings
Simple use cases are shown below (Figure 18). A basic example includes LaTeX strings for x-y labels and legends:
function LaTeX_Strings()
x = 0:0.05:4π
lines(x, x -> sin(3x) / (cos(x) + 2) / x; label=L"\frac{\sin(3x)}{x(\cos(x)+2)}",
figure=(; resolution=(600, 400)), axis=(; xlabel=L"x"))
lines!(x, x -> cos(x) / x; label=L"\cos(x)/x")
lines!(x, x -> exp(-x); label=L"e^{-x}")
limits!(-0.5, 13, -0.6, 1.05)
axislegend(L"f(x)")
current_figure()
end
with_theme(LaTeX_Strings, publication_theme())
Figure 18: Plot with LaTeX strings.
A more involved example will be one with some equation as text
and increasing legend numbering for curves in a plot:
function multiple_lines()
x = collect(0:10)
fig = Figure(resolution=(600, 400), font="CMU Serif")
ax = Axis(fig[1, 1], xlabel=L"x", ylabel=L"f(x,a)")
for i = 0:10
lines!(ax, x, i .* x; label=latexstring("$(i) x"))
end
axislegend(L"f(x)"; position=:lt, nbanks=2, labelsize=14)
text!(L"f(x,a) = ax", position=(4, 80))
fig
end
JDS.multiple_lines()
Figure 19: Multiple lines.
Where latexstring
from LaTeXStrings.jl
has been used to parse the string. An alternative to this simple case is L"%$i x"
, which is used in the next example.
But, before that there is another problem, some lines have repeated colors and that’s no good. Adding some markers and line styles usually helps. So, let’s do that using Cycles for these types. Setting covary=true
allows to cycle all elements together:
function multiple_scatters_and_lines()
x = collect(0:10)
cycle = Cycle([:color, :linestyle, :marker], covary=true)
set_theme!(Lines=(cycle=cycle,), Scatter=(cycle=cycle,))
fig = Figure(resolution=(600, 400), font="CMU Serif")
ax = Axis(fig[1, 1], xlabel=L"x", ylabel=L"f(x,a)")
for i in x
lines!(ax, x, i .* x; label=L"%$i x")
scatter!(ax, x, i .* x; markersize=13, strokewidth=0.25,
label=L"%$i x")
end
axislegend(L"f(x)"; merge=true, position=:lt, nbanks=2, labelsize=14)
text!(L"f(x,a) = ax", position=(4, 80))
set_theme!() # reset to default theme
fig
end
JDS.multiple_scatters_and_lines()
Figure 20: Multiple Scatters and Lines.
And voilà. A publication quality plot is here. What more can we ask for? Well, what about different default colors or palettes. In our next section, we will see how to use again Cycles and know a little bit more about them, plus some additional keywords in order to achieve this.
Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso