1

How to set fonts of labels? Everything in graph is plotted with Latin Modern Roman, but labels not. I tried csfont, but it didn't help.

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.rcParams["font.family"] = "Latin Modern Roman"
csfont = {'fontname':'Latin Modern Roman'}

plt.xlabel(r'$\it{Label2}$', fontsize=14)
plt.ylabel(r'$\it{Label1}$ (eV)', fontsize=14, **csfont)

I tried this for ylabel:

plt.ylabel(r'$\it{Excitační energie}$ (eV)', family='Latin Modern Roman', fontsize=14)

enter image description here

and this is result:

Arrara
  • 173
  • 1
  • 10

1 Answers1

2

You can pass a fontdict with the font parameters, specifying the 'family' with the font you want to use:

font = {'family': 'Latin Modern Roman',
    'color':  'darkred',
    'weight': 'normal',
    'size': 16,
    }
plt.ylabel('label-text', fontdict=font)

or set the family in the label:

plt.ylabel('label-text', family='Latin Modern Roman')

There's a full example in the docs using fontdict

Matts
  • 1,301
  • 11
  • 30
  • I tried it and the result is in my question - first part of ylabel is not the setted font. – Arrara Oct 05 '19 at 13:26
  • It works for me with a different font, as I don't have this font installed. Maybe take a look at this [answer](https://stackoverflow.com/questions/7726852/how-to-use-a-random-otf-or-ttf-font-in-matplotlib) if you need to point it to the font file. – Matts Oct 05 '19 at 13:32