# Typing special characters in Plot legends and titles

**URL:** <https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [May 30, 2019, 2:28pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771 "2019-05-30T14:28:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 30, 2019, 2:28pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/1 "2019-05-30T14:28:13Z")

</div>

I still didn’t find a practical way to type special characters in to a plot legend. In particular, I am trying to type an Angstrom (Å) character in an axis label.

The situation is the following:

In the REPL I can use the unicode substitution and type \AA or \Angstrom, which is replaced by the symbol, and appears correctly in the GR plot, and I can save the figure nicely to PNG or PDF.

This solution appeared to be fine, except that I cannot find a way to do the same thing in a script. The code:

```julia
using Plots
plot(xlabel="\\AA")
savefig("test.png")

```

returns `\AA: undefined symbol`

If that option worked I think it would be the optimal solution.

I read some threads and it was suggested to use LaTeXStrings. However, I cannot get an acceptable result with it either, and this might be because of the poor documentation of the package, or perhaps I am missing something obvious. In particular, I try:

```julia
plot(xlabel=L"\AA")

```

but that gives me the Angstrom sign in LaTeX equation mode (italics), which is not what I need. I tried

```julia
plot(xlabel=L"\textrm{\AA}")

```

this works, but the font of the Angstrom sign is different from the fonts of the other text of the plot. I could not find a solution to obtain the same fonts when using the latex strings package.

In other words, I still cannot find a way to produce a publication quality plot with Plots with special characters. The use of the LaTeX code seems to be the most general solution, but in this case a practical way to adjust the fonts of the plot to the fonts is needed. As a simple alternative workaround for simple characters, it would be nice if the unicode mapping worked with scripts as it works in the REPL.

I appreciate any help.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 30, 2019, 2:57pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/2 "2019-05-30T14:57:54Z")

</div>

I normally do one of two approaches

1. Use pyplot as backend and the call matplotlib2tikz
2. Use pgfplots as backend and save directly as tex

I prefer approach 1 whenever it works as it produces far better and readable tikz code.

Here’s the code I normally use to save a tikz figure using matplotlib2tikz

```julia
using PyCall
PyCall.@pyimport matplotlib2tikz
"""
`savetikz(path; fig = PyPlot.gcf(), extra::Vector{String})`
"""
function savetikz(path; fig = PyPlot.gcf(), extra=[])
    if extra == []
        matplotlib2tikz.save(path,fig, figureheight = "\\figureheight", figurewidth = "\\figurewidth")
    else
        matplotlib2tikz.save(path,fig, figureheight = "\\figureheight", figurewidth = "\\figurewidth", extra_tikzpicture_parameters = PyCall.pybuiltin("set")(extra))
    end
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 4, 2019, 5:12pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/3 "2019-09-04T17:12:32Z")

</div>

I am still in search for a good solution for this problem. Here goes a more detailed description:

I am trying to use LaTeXStrings to add special characters to the label of a plot. The following results can be obtained:

`histogram!(xlabel=L"Test \alpha")`

Result: [![image](https://global.discourse-cdn.com/julialang/original/3X/f/b/fb0108beabeaa9ff79c49ff32ab729c686f90efe.png)](https://user-images.githubusercontent.com/31046348/64274988-f3ecea00-cf1a-11e9-8dd2-c2c7275a4ecd.png)

All text is interpreted as an equation, which is not satisfactory.

`histogram!(xlabel=L"\textrm{Test }\alpha")`

Result: [![image](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5f781d1f7ef51a2a50d56ec130475c328242b14.png)](https://user-images.githubusercontent.com/31046348/64275089-272f7900-cf1b-11e9-8f91-efe736503bfc.png)

Possibly the best alternative, but the fonts are different from the fonts of the rest of the plot.

`histogram!(xlabel="Test $(L"\alpha")")`

Result:  
[![image](https://global.discourse-cdn.com/julialang/original/3X/e/1/e13d220d417335ea8714bce4c4249149ab10c687.png)](https://user-images.githubusercontent.com/31046348/64275213-7b3a5d80-cf1b-11e9-990f-4b21ef8ffacd.png)

This one looks very promising, because the special character (alpha) is rendered, and the fonts are not changed. However, the dollar signs appear. I could not get rid of them.

I am not sure if this an issue of the package or if there is a solution. I was able to find the following workaround for some characeters:

```julia
string="$(L"Test \alpha")"
histogram!(xlabel=string[2:length(string)-1])

```

Result:  
[![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/842fff115466550e3d0b9e13af8847c7c5051854.png)](https://user-images.githubusercontent.com/31046348/64275835-e2a4dd00-cf1c-11e9-8d44-0d827d7b1e8f.png)

(unfortunately this option does not work to produce the Angstrom character (\AA))

Any thoughts? I could not find help anywhere else.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 4, 2019, 7:34pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/4 "2019-09-04T19:34:09Z")

</div>

I can’t test it right now, but maybe `sprintf` the Angstrom symbol as you did in Latex mode and then use the resulting string as the label?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2019, 8:10pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/5 "2019-09-04T20:10:01Z")

</div>

> [@lmiq](#):
>
> `histogram!(xlabel=L"Test \alpha")`

You want `histogram!(xlabel=L"Test $\alpha$")`

---

<div class="post-metadata">

**Author:** ![dalejordan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dalejordan/32/2196_2.png) [@dalejordan](https://discourse.julialang.org/u/dalejordan)\
**Post date:** [September 4, 2019, 8:23pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/6 "2019-09-04T20:23:12Z")

</div>

Can anyone shed light on what the root problem is with just displaying Unicode in strings? Do backends not support Unicode or is Plots dropping the ball? Or what?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 4, 2019, 8:24pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/7 "2019-09-04T20:24:22Z")

</div>

Thanks, but, at least here, it returns ![64275213-7b3a5d80-cf1b-11e9-990f-4b21ef8ffacd](https://global.discourse-cdn.com/julialang/original/3X/e/1/e13d220d417335ea8714bce4c4249149ab10c687.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2019, 8:31pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/8 "2019-09-04T20:31:45Z")

</div>

What plots backend are you using?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 4, 2019, 8:33pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/9 "2019-09-04T20:33:41Z")

</div>

I suppose it is “GR” (I didn’t set anything manually)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2019, 8:34pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/10 "2019-09-04T20:34:45Z")

</div>

It should work with PyPlot. I don’t think GR really supports LaTeX.  
[https://github.com/jheinen/GR.jl/issues/198](https://github.com/jheinen/GR.jl/issues/198)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2019, 8:45pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/11 "2019-09-04T20:45:13Z")

</div>

> [@dalejordan](#):
>
> Can anyone shed light on what the root problem is with just displaying Unicode in strings? Do backends not support Unicode or is Plots dropping the ball? Or what?

Plots is Latin1 by default with the GR backend at least, due in part to limitations of the backend:

> <https://github.com/TuringLang/MCMCChains.jl/issues/111>
>
> Sorry for yet another question. My parameter names contain Greek letters, e.g. \<…img src="https://latex.codecogs.com/svg.latex?\\Large&space;\\alpha" /\> or \<img src="https://latex.codecogs.com/svg.latex?\\Large&space;\\rho\_A" /\>. In standard Julia plots, I can pass a \[LaTeXString\](https://github.com/stevengj/LaTeXStrings.jl) for labelling. Is this also possible here or would I have to write my own plotting code for the chains? Or do you recommend a different approach?

Even if a backend supports Unicode strings, its text rendering may not be capable of displaying many characters (due to limited font coverage, lack of support for combining characters, lack of support for fallback fonts for missing glyphs…). See here for a summary:

> <https://github.com/JuliaPlots/Plots.jl/issues/791>
>
> \`\`\`julia
> using Plots; gr()
> plot(x-\>x, xlabel="λ")
> \`\`\`
> \>BoundsError: attempt …to access 1-element Array{UInt8,1} at index \[2\]
> 
> \> in setindex!(::Array{UInt8,1}, ::Int64, ::Int64) at ./array.jl:415
> in latin1(::String) at /home/juliohm/.julia/v0.5/GR/src/GR.jl:456
> in text at /home/juliohm/.julia/v0.5/GR/src/GR.jl:488 \[inlined\]
> in gr\_text(::Float64, ::Float64, ::String) at /home/juliohm/.julia/v0.5/Plots/src/backends/gr.jl:191
> in gr\_display(::Plots.Subplot{Plots.GRBackend}, ::Measures.Length{:mm,Float64}, ::Measures.Length{:mm,Float64}, ::Array{Float64,1}) at /home/juliohm/.julia/v0.5/Plots/src/backends/gr.jl:711
> in gr\_display(::Plots.Plot{Plots.GRBackend}) at /home/juliohm/.julia/v0.5/Plots/src/backends/gr.jl:494
> in \_show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/svg+xml")}, ::Plots.Plot{Plots.GRBackend}) at /home/juliohm/.julia/v0.5/Plots/src/backends/gr.jl:1089
> in show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/svg+xml")}, ::Plots.Plot{Plots.GRBackend}) at /home/juliohm/.julia/v0.5/Plots/src/output.jl:197
> in show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("text/html")}, ::Plots.Plot{Plots.GRBackend}) at /home/juliohm/.julia/v0.5/Plots/src/output.jl:177
> in show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::String, ::Plots.Plot{Plots.GRBackend}) at ./multimedia.jl:33
> in #sprint#304(::Void, ::Function, ::Int64, ::Function, ::String, ::Vararg{Any,N}) at ./strings/io.jl:37
> in display\_dict(::Plots.Plot{Plots.GRBackend}) at /home/juliohm/.julia/v0.5/Plots/src/output.jl:266
> in execute\_request(::ZMQ.Socket, ::IJulia.Msg) at /home/juliohm/.julia/v0.5/IJulia/src/execute\_request.jl:187
> in eventloop(::ZMQ.Socket) at /home/juliohm/.julia/v0.5/IJulia/src/eventloop.jl:8
> in (::IJulia.##13#19)() at ./task.jl:360

The solution is for backends to switch to a modern full-featured text renderer. As explained in the issue above, HarfBuzz seems to be a promising cross-platform option.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 4, 2019, 8:48pm UTC](https://discourse.julialang.org/t/typing-special-characters-in-plot-legends-and-titles/24771/12 "2019-09-04T20:48:02Z")

</div>

`histogram!(xlabel=L"C$\alpha$ distance to surface / $\mathrm{\AA}$")`

With pyplot() it worked, I add the need to use \mathrm to obtain an angstrom sign not in italics.

The plot are not as pretty as with GR, but this is a solution.
