PlotlyJS + LaTeXStrings error

Hi everyone,

I am having trouble producing a plot using PlotlyJS and LaTeXStrings.
Here it is a simplified version of the code:

using DataFrames, PlotlyJS, LaTeXStrings

df = DataFrame(
    country = [repeat(["Portugal"], 15); repeat(["USA"], 15); repeat(["Sweden"], 15)],
    year = repeat(1:15, 3),
    r = randn(15*3)
)
sort!(df,:year)

plot(df,
	x = :year,
	y = :r,
	color = :country,
	labels = Dict(:country => "Countries"),
	Layout(
		font_size=14,
		margin=attr(l=80, r=60, b=60, t=80),
		title = "My Plot",
		xaxis_title = "",
		yaxis_title = L"$r$"
	)
)

The error I am getting is:
ERROR: MethodError: no method matching get(::LaTeXString, ::Symbol, ::Dict{Any, Any})

Does anyone have a clue on how to solve it?
I saw other discussions where people have trouble getting the math shown up properly. But no one with the same error as me.

1 Like

The error comes from here:

since PlotlyBase doesn’t understand how to get the text from a LaTeXString. You can do this “manually” with a LaTeXString by appending .s e.g. L"r".s
Also, you don’t need to wrap the expression in $ signs, this is done by the LaTeXStrings package for you.

The only time I’ve been able to see LaTeX from PlotlyJS.jl is by rendering the plot in a browser (that is, via html): so I would run code like this from a Julia REPL session in a terminal:

Plot(df,  # Note: using `Plot`, not `plot`, from the terminal
	x = :year,
	y = :r,
	color = :country,
	labels = Dict(:country => "Countries"),
	Layout(
		font_size=14,
		margin=attr(l=80, r=60, b=60, t=80),
		title = "My Plot",
		xaxis_title = L"\sqrt{N_c}".s,
		yaxis_title = L"\int r".s # Note the `.s` suffix
	)
)

Changes are noted.

3 Likes

@jd-foster thank you very much! It worked perfectly.

1 Like

@jd-foster Actually, I have another question that maybe you can help me with.
As you said, we cannot render the plot with LaTeX unless we do it through a browser.
That is not a big issue for me if at least I am able to save it correctly, namely in PDF or SVG formats.
However, when using savefig() from PlotlyJS, in whatever format I choose, I am getting the same problem: math does not show up correctly. Do you know any way to overcome the problem?

This is a good question. I do not think it is possible with the current code in PlotlyJS.jl, but would be happy to be corrected.
The best work-around I can think of is to save it as in .html format, e.g.

savefig(p, "countries.html")

then convert the html somehow to the desired format.

The math (LaTeX) symbols are saved as expected, only if you provide the unicode for each symbol, either like this:

fig = Plot(scatter(x=1:5, y=rand(5), mode="lines"), Layout(width=390, height=300,
        xaxis_title="μ²/3", yaxis_title="ξ"))

or:

fig = Plot(scatter(x=1:5, y=rand(5), mode="lines"), Layout(width=390, height=300,
        xaxis_title="\u03bc²/3", yaxis_title="\u03be"))

Then the corresponding plot is saved OK in pdf, svg, png format.
LE: For a more LaTeX looking text, you may set , font_family="Open Sherif" or
font_family="Balto".

3 Likes

Thank you @empet. Your solution actually solves most of my needs, but I think it is just a nice workaround and not a fully satisfying solution.
That way we cannot use more complicated expressions like \sqrt{2 + x}.
Checking LaTeXStrings.jl GitHub I found a FAQ regarding this issue: the package is not responsible for rendering the math.
Using Pluto.jl Notebooks, the math is rendered perfectly once you append .s to the LaTeXString. But using the function savefig() from PlotlyJS.jl still does not save the figure with math (I tried to save it as PDF, SVG and PNG and none of the formats worked…).

I don’t know why your plots are not saved with unicode. Here are my saved files:
pdf: https://github.com/empet/Datasets/blob/master/Images/save-unicode1.pdf
and png:
save-unicode1

I believe the issue is about rendering the LaTeX commands, and having them saved in the static image format. Having Unicode characters render in saved images should not be a problem.

I have figured out how to consistently have LaTeX appear in images produced using savefig(), but it requires modifying the source code of PlotlyJS.jl itself. If you would like to try this option, you can use the fork of the code at my GitHub here by doing

 pkg> add https://github.com/jd-foster/PlotlyJS.jl.git#jdf/kaleido-mathjax-on
3 Likes