3

Before upgrading my matplotlib library (macOS), I had the following latex font in my figure's texts : enter image description here

However after the update, I am getting the following font: enter image description here

In both codes, i am using the following command: plt.title(r'$L_x = 1\mathrm{m}, \ \phi_{in}=1$'). Can you please help me get the back the font of the first figure?

Many thanks

outlaw
  • 241
  • 4
  • 16

2 Answers2

3

MathText has its own fontset. You can set it via the rcParam mathtext.fontset.

#mathtext.fontset : dejavusans ## Should be 'dejavusans' (default),
                               ## 'dejavuserif', 'cm' (Computer Modern), 'stix',
                               ## 'stixsans' or 'custom'

To get a serif MathText, use, dejavuserif, cm, or stix.

Example

plt.rcParams["mathtext.fontset"] = "stix"
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
1

Add the following lines before your plot

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)

You may have to dig a bit to find the correct font though, maybe your original one was not 'sans-serif'.

Christian
  • 1,162
  • 12
  • 21
  • Hi, thanks for your reply. I got the following error: OSError: [Errno 2] No such file or directory: 'latex' – outlaw Apr 13 '18 at 08:12
  • Do you have latex installed on your computer ? You can see that, typing `which latex` in a terminal. Anyway, you can then just remove my last proposed line (hence, remove `rc('text', usetex=True)` . – Christian Apr 13 '18 at 08:16
  • Yes I have tex live installed on my mac, however when I type 'which latex' in the terminal I dont get an output. Also when I remove the line you mentioned i get the following error: UserWarning: findfont: Font family [u'sans-serif'] not found. Falling back to DejaVu Sans (prop.get_family(), self.defaultFamily[fontext])) – outlaw Apr 13 '18 at 08:20
  • mmmmh this is weird. The update probably broke something with latex and/or matplotlib. I see two main options (1) the code I gave without the last line works, but cannot find the font 'sans-serif', you can try to find one that works (maybe 'helvetica', 'times new roman', ...) (2) try to reinstall latex and see if that fixes the problem with or without my code. – Christian Apr 13 '18 at 08:25
  • [here](https://stackoverflow.com/a/8755818/3283333) an interesting answer to check which fonts are available for matplotlib. – Christian Apr 13 '18 at 08:39
  • will check them, seems interesting – outlaw Apr 13 '18 at 12:09