# Changing font style for latex rendering using PyPlot

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:

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:

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

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

1 Like

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.

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

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

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:

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.

xlabel("foo", fontname="serif", fontsize="24") works for me. In general, you can pass any text properties as arguments to xlabel

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.

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

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")

1 Like

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

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

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


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

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.