LaTeX code for titles, labels,... with Plots.jl

question
package
plotting

#1

I am using Plots.jl for making plots, but I would like to have my legend, title, labels,… in latex typesetting. With other libraries I know I can just use L"…", but when I run the following code:

using Plots
plot(0:2,0:2, xlab = L"x")

I get the following error:

LoadError: UndefVarError: @L_str not defined

Optimally I would also like the numbers on the x-and y-axis to be LaTeX, therefore I know I can use the package PGFPlots, but this is still being developped for Plots.jl.


#2

You need using LatexStrings


#3

This worked, thanks. Any idea how to set the numbers on the x/y axis to LaTeX?


#4

try xticks?


#5

Works like a charm, for completeness, my code is now:

using Plots
using LaTeXStrings

p = plot(0:2,0:2, xlab = L"x^2", xticks = ([0,1,2],[L"0", L"1", L"2"]))

and my problem has been solved completely.
Thanks!


#6

Which backends does this work with?


#7

I have it working on all that I have installed (gr, plotlyjs, pyplot)
EDIT: wait, it sometimes fails on plotlyjs, though it works most times. Peculiar.


#8

Either plotly() and plotlyjs() backend doesn’t work for me
Maybe it has something to do with OS language?


#9

I am on OS X - I get the same occasionally, but most times it works with plotlyjs(). It is bit strange.


#10

@mkborregaard The correct capitalization of the package name is LaTeXStrings.


#11

Indeed it is, thanks for the dilligence.


#12

Note: you don’t need the LaTeXStrings package in order to render LaTeX in Plots.jl - it just makes doing so more convenient. You can use LaTeX directly in Plots.jl by manually escaping all the $s, \s, etc.


#14

Hello, how would you use LaTeX in Plots without the LaTeXStrings package ? label="${\bar{fitted}} prices$" or label=L"{\bar{fitted}} prices" would not work…

EDIT: Sorry… got it: label="\${\\overline{fitted}} \\thinspace prices\$")


#15

Sorry for the reviving this thread again but I’m struggling to get this right:

using Plots, LaTeXStrings
plot(rand(100))
title!("This is an alpha: α")                   # produces ? for alpha
title!("This is an alpha: \$\\alpha\$")         # produces $ α$
title!(L"This is an alpha: α")                  # produces nothing
title!(L"This is an alpha: \alpha")             # produces nothing
title!(L"This is an alpha: $\alpha$")           # produces $ α$

I can’t seem to find a combination that plots the Latex symbol without also plotting the dollar signs - any help appreciated.


#16

In my (small) experience, it depends on the backend. If you’re using the default for Plots.jl (GR.jl), LaTeX doesn’t work very well. It only works if you’re using a string that is all LaTeX, e.g.:

title!("\$\\alpha\$")

Otherwise you should currently use a different backend. For me, PyPlot.jl usually does its job with LaTeX.


#17

…agree wrt. PyPlot, but on my computer (Win10), PyPlot doesn’t work in Juno – for some reason.


#18

Hi Nils,

I found that for the title and gr() backend one has to use the latexstring() function directly instead of L"some latex code".

Below an example code

using Plots, LaTeXStrings

gr()

date = "07-08-2018"
titlestring = latexstring("\\mathrm{My\\, \\alpha\\, including\\, a\\, variable\\, date:\\,}", date)

plot(sin, (0:0.01:5*pi), dpi=300,
     title = titlestring,
     xlabel = L"\mathrm{My\, x-label\, (mJ/cm^{3})}",
     ylabel = L"\mathrm{\alpha (m/s)}")

Rob.