# Computer modern font in every element of plot (PyPlot)

**URL:** <https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030>\
**Category:** Visualization\
**Tags:** pyplot, fonts\
**Created:** [November 5, 2021, 10:25pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030 "2021-11-05T22:25:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveR](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@DaveR](https://discourse.julialang.org/u/DaveR)\
**Post date:** [November 5, 2021, 10:25pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/1 "2021-11-05T22:25:08Z")

</div>

I’m trying to achieve an homogeneous looking plot where every element of the plot has Computer Modern font using the PyPlot package.  
Following some of the answers to existing questions in the forum, this is as close as I’ve gotten:

```julia
using PyPlot
using LaTeXStrings

PyPlot.matplotlib[:rc]("text", usetex=false) 
PyPlot.matplotlib[:rc]("mathtext",fontset="cm")
PyPlot.matplotlib[:rc]("font",family="serif",size=12)

x = range(0; stop=2*pi, length=1000); y = sin.(3 * x + 4 * cos.(2 * x))
plot(x, y, color="red", linewidth=2.0, linestyle="--")
title(L"\sin(3x + 4\cos(2x))")
xlabel(L"x")
ylabel(L"y");

```

With the following output

![image](https://global.discourse-cdn.com/julialang/original/3X/6/8/68dc286c905037267e0debe3df15b0fe78c9b655.png)

But the ticks’s numbers aren’t in Computer Modern font. Additionally, setting

```julia
PyPlot.matplotlib[:rc]("text", usetex=true) 

```

disables the inline display somehow.

Thanks in advance.

---

<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:** [November 6, 2021, 12:59am UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/2 "2021-11-06T00:59:32Z")

</div>

> [@DaveR](#):
>
> ```julia
> x = range(0; stop=2*pi, length=1000); y = sin.(3 * x + 4 * cos.(2 * x))
> plot(x, y, color="red", linewidth=2.0, linestyle="--")
> title(L"\sin(3x + 4\cos(2x))")
> xlabel(L"x")
> ylabel(L"y");
> 
> ```

With the GR backend you can get that with:

```julia
julia> using Plots, LaTeXStrings

julia> default(fontfamily="Computer Modern",framestyle=:box)

julia> plot(x, y, color="red", linewidth=2.0, linestyle=:dash, label=L"y", title=L"\sin(3x + 4\cos(2x))", xlabel=L"x", ylabel="y")

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc25e1c2606021d86935c7fdaafcef1397fea3a1.png)

see: [Nice fonts with Plots, GR and LaTeXStrings](https://discourse.julialang.org/t/nice-fonts-with-plots-gr-and-latexstrings/60037)

---

<div class="post-metadata">

**Author:** ![DaveR](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@DaveR](https://discourse.julialang.org/u/DaveR)\
**Post date:** [November 6, 2021, 1:04am UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/3 "2021-11-06T01:04:51Z")

</div>

Thanks for replying! I saw your post earlier today and I just wanted to know if what you did is doable using PyPlot.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 6, 2021, 8:18am UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/4 "2021-11-06T08:18:48Z")

</div>

Setting this line to `true`:

```julia
PyPlot.matplotlib[:rc]("text", usetex=true)

```

Seems to fix the issue on external plot window:  
 ![PyPlot_CM_ComputerModern_font](https://global.discourse-cdn.com/julialang/original/3X/0/9/09a4d7190dec9067ef9e4826b1909b5f81a8a52f.png)

Not sure what you mean by inline plot but [linking this thread](https://discourse.julialang.org/t/changing-font-style-for-latex-rendering-using-pyplot/5910/11), which is rich in related info.

---

<div class="post-metadata">

**Author:** ![DaveR](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@DaveR](https://discourse.julialang.org/u/DaveR)\
**Post date:** [November 6, 2021, 2:23pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/5 "2021-11-06T14:23:36Z")

</div>

By inline plots I refer to the plots generated in a Jupyter Notebook’s cell. Setting that line to true in a Jupyter Notebook session just results in the following message

`Figure(PyObject <Figure size 640x480 with 1 Axes>)`

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [November 6, 2021, 2:51pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/6 "2021-11-06T14:51:22Z")

</div>

In PyPlot, you can choose the serif font using the `font.serif` option. In particular, if you want Computer Modern, you can set this option to `"cmr10"`:

```julia
PyPlot.matplotlib[:rc]("text", usetex=false) 
PyPlot.matplotlib[:rc]("mathtext", fontset="cm")
PyPlot.matplotlib[:rc]("font", family="serif", serif="cmr10", size=12)

```

If I understand correctly, this uses the `cmr10.ttf` font file bundled with matplotlib.

---

<div class="post-metadata">

**Author:** ![DaveR](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@DaveR](https://discourse.julialang.org/u/DaveR)\
**Post date:** [November 6, 2021, 3:36pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/7 "2021-11-06T15:36:46Z")

</div>

That does the trick, thanks! Unfortunately, there is a bug inside matplotlib when using cmr10:

`RuntimeWarning: Glyph 8722 missing from current font`

![image](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f00b95eeb502175842c29f0cdf8cde8823ae0ef.png)

It has been reported [here](https://github.com/matplotlib/matplotlib/issues/17007), but the fix doesn’t look easy to implement.

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [November 6, 2021, 4:57pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/8 "2021-11-06T16:57:09Z")

</div>

This is a possible workaround (which will fix the issue for tick labels):

```julia
PyPlot.matplotlib[:rc]("axes.formatter", use_mathtext = true)

```

---

<div class="post-metadata">

**Author:** ![DaveR](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@DaveR](https://discourse.julialang.org/u/DaveR)\
**Post date:** [November 7, 2021, 3:29pm UTC](https://discourse.julialang.org/t/computer-modern-font-in-every-element-of-plot-pyplot/71030/9 "2021-11-07T15:29:49Z")

</div>

Doing that gives the following warning

`Font 'default' does not have a glyph for '\u2212' [U+2212], substituting with a dummy symbol.`

According to the discussion in the GitHub issue, after doing that you’d need to tell matplotlib to get the minus sign from some other font file. That’s what I wouldn’t know how to do.
