# Changing font style for latex rendering using PyPlot

**URL:** <https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910>\
**Category:** Visualization\
**Tags:** pyplot, fonts\
**Created:** [September 15, 2017, 7:20pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910 "2017-09-15T19:20:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [September 15, 2017, 7:20pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/1 "2017-09-15T19:20:03Z")

</div>

Hi all,

I am trying to make plots, using PyPlot, that have latex equations. Does anyone know how to change the font of the default latex render in PyPlot ? Right now I am using the following:

```julia
PyPlot.matplotlib[:rc]("text", usetex=false) # allow tex rendering
rc("font", family="sans-serif", weight="normal", size="18")
rc("axes", labelsize="18")

```

If I write for example:

```julia
xlabel(L"Linear")

```

I get “_Linear_” where I would like to have just “Linear”

The same is valid if I write:

`xlabel(L"2x")`

then I get “_2x_” where I would like to have “2x”.

thank you

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [September 16, 2017, 2:38am UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/2 "2017-09-16T02:38:03Z")

</div>

The `L"Linear"` form makes PyPlot treat the whole string as a math expression (as if surrounded by dollar signs in \LaTeX). Either use `"Linear"` or (to embed normal text in a math expression) `L"\mathrm{Linear}"`.

This is not iron-clad: if you specify `rc("font",style="italic")`, you may not be able to get away from italics at all.

The reference is [http://matplotlib.org/users/mathtext.html](http://matplotlib.org/users/mathtext.html)

---

<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 16, 2017, 2:49am UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/3 "2017-09-16T02:49:31Z")

</div>

If you don’t want an equation, you just shouldn’t use L"…" at all. Just use an ordinary string.

All L"…" does is allow you to enter equations without having to insert tons of backslash escapes, and to allow you to optionally omit the .... There is no reason to use it if you don’t want an equation.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [September 16, 2017, 9:04am UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/4 "2017-09-16T09:04:44Z")

</div>

Well - except to match fonts. In this case just use L"\text{blah}" or possibly L"{\rm blah}" - I didn’t test this but something along those lines will work

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [September 19, 2017, 11:30pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/5 "2017-09-19T23:30:51Z")

</div>

> [@Ralph\_Smith](#):
>
> [http://matplotlib.org/users/mathtext.html](http://matplotlib.org/users/mathtext.html)

thank you, this answer solved my problem. What I was looking for was something like: `L"\mathsf{<b> / <b_0>}"` for example

---

<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 18, 2018, 8:43pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/6 "2018-09-18T20:43:45Z")

</div>

Continuing the thread: I would like to change the label fonts to the default (Computer Modern, it appears) font of Latex.

I am trying something like this:

```julia
using LaTeXStrings
using PyPlot
x = [0, 1]
y = [0, 1]
rc("font",family="serif")
xlabel(L"x")
ylabel(L"y")
plot(x,y)
savefig("plot.pdf")

```

It doesn´t matter which font family I set, the output is always the same, and the font of the “x” looks like DejaVu Sans. I searched quite a bit how to do that and tried many things, but with no luck. A minimal working example of a font change would be of great help!

Thank you.

**EDIT: I noticed now that that is changing the font of the tick numbers, but not the font of the labels.**

---

<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 18, 2018, 9:48pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/7 "2018-09-18T21:48:15Z")

</div>

`xlabel("foo", fontname="serif", fontsize="24")` works for me. In general, you can pass any [text properties](https://matplotlib.org/api/text_api.html#matplotlib.text.Text) as arguments to `xlabel`

---

<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 18, 2018, 10:02pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/8 "2018-09-18T22:02:54Z")

</div>

Apparently some text properties do not work using the LaTeXStrings attribute, “L”:

`xlabel(L"foo", fontname="serif", fontsize="24")`

I can change the size, but not the font type.

---

<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 19, 2018, 12:49am UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/9 "2018-09-19T00:49:44Z")

</div>

> <https://stackoverflow.com/questions/43876143/mathjax-matplotlib-font-in-jupyter>

---

<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 19, 2018, 11:02am UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/10 "2018-09-19T11:02:44Z")

</div>

EDIT: Found a solution! Which is using `rc("text",usetex="True")`,  
as in the following example:

```julia
using LaTeXStrings
using PyPlot
x = [0, 1]
y = [0, 1]
rc("text",usetex="True")
xlabel(L"x")
ylabel(L"y")
plot(x,y)
savefig("plot.pdf")

```

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [September 19, 2018, 2:16pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/11 "2018-09-19T14:16:25Z")

</div>

You can also try this (seems to work well also in IJulia)

```julia
PyPlot.matplotlib[:rc]("mathtext",fontset="cm") #computer modern font 
PyPlot.matplotlib[:rc]("font",family="serif",size=12) #font similar to LaTeX
figure(figsize=(7,5))
  plot(x,y)
  title(L"a title using LaTeX, $2 b^2 + 0.5$")
  xlabel(L"$x$")
  ylabel("function value")
  text(-2.5,0.9,L"some text, $\ln(\mathrm{loss})$")
  text(-2.5,5,L"$\mu_2 = \int x^2 f(x) dx$")

```

In case you like TImes-Roman, try this

```julia
PyPlot.matplotlib[:rc]("mathtext",fontset="stix")
PyPlot.matplotlib[:rc]("font",family="STIXGeneral",style="normal",size=18)
...

```

---

<div class="post-metadata">

**Author:** ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)\
**Post date:** [September 23, 2018, 6:36pm UTC](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/12 "2018-09-23T18:36:40Z")

</div>

Adding my method for those also using **Seaborn** on top of **matplotlib** :

```julia
using PyPlot; using LaTeXStrings; using PyCall; @pyimport seaborn as sns;

rc_mod = Dict("mathtext.default" => "regular",
              "pgf.preamble" => [raw"\usepackage[detect-all,locale=DE]{siunitx}"])
                        
sns.set(rc=rc_mod )

```

Still can’t figure out how to load in custom latex packages though…

The key `text.latex.preamble` doesn’t seem to work.
