0

I use latex in matplotlib by setting

plt.rcParams.update({'mathtext.fontset': 'stix'})
plt.rcParams.update({'font.family': 'STIXGeneral'})

I am using the letter $\ell$ very often in my research and there is a small detail bothering me. As you can see below, matplotlib renders the symbol with the little loop smaller and the letter more slanted. To me it almost looks like a vertically stretched $e$. I tried using the "\mathrm{\ell}" command instead but it did not change anything. Is there any way I could get the symbol to look normal?

enter image description here

PS: it looks like stackoverflow is not detecting the math mode $ for some reason. If you know how to fix it (or if I am doing something wrong) please point it out or edit the question. Thanks!

Luismi98
  • 282
  • 3
  • 14
  • 1
    Quick question, are you using the same font on overleaf and matplotlib? Can you get overleaf to produce the same symbol as matplotlib? This could be useful to diagnose your issue. – K.Cl Apr 16 '21 at 20:43
  • @K.Cl That is a good point, and not I was not using the same font! – Luismi98 Apr 16 '21 at 21:45

2 Answers2

1

The reason is the font you are using in matplotlib. With the following settings, for example, you get the same letter as in overleaf:

import matplotlib.pyplot as plt
import numpy as np

# Example data
t = np.arange(0.0, 10, 1)
s = np.arange(0.0, 10, 1)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.xlabel(r'$\ell$', fontsize=30)
plt.ylabel(r'$\ell$', fontsize=30)

plt.plot(t, s)
plt.show()

You get:

matplotlib plot

However, In Jupyterlab I could not reproduce. It used the overleaf fonts even with your settings.

jf_
  • 3,099
  • 2
  • 13
  • 31
  • Thanks for your answer. Trying your code led me to a ```RuntimeError: Failed to process string with tex because latex could not be found``` [error] (https://stackoverflow.com/questions/58121461) because it seems like I don't have the appropiate packages installed. – Luismi98 Apr 16 '21 at 21:48
  • I use the built-in [mathtext](https://matplotlib.org/stable/tutorials/text/mathtext.html) package from matplotlib instead. Your answer helped me to find the right solution for me changing just the font type – Luismi98 Apr 16 '21 at 21:51
0

This proved to be the simplest solution for me. Thanks the others for pointing out the font being the issue.

Rather than

plt.rcParams.update({'mathtext.fontset': 'stix'})
plt.rcParams.update({'font.family': 'STIXGeneral'})

I now write the first line as

plt.rcParams.update({'mathtext.fontset': 'cm'})

which works like charm. This is helpful if you are someone like me not using TeX but just the mathtext matplotlib built-in function.

Luismi98
  • 282
  • 3
  • 14