Cactus Philosopher
Cactus Philosopher

Reputation: 864

matplotlib - mathtext italics not in desired font

I'm trying to get the italics font to match the font used for the rest of the axes labels but its not working as expected.

import matplotlib.pyplot as plt
import numpy as np


plt.rcParams['font.family'] = 'Garuda'
#solution is to change mathtext italics as suggested below
plt.rcParams['mathtext.it']= 'Garuda:italic'
plt.rcParams['mathtext.cal']= 'Garuda:italic'
plt.rcParams['mathtext.default'] = 'regular'
plt.rcParams["mathtext.fontset"] = 'custom'


x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Plot $\it{ugly}$ $\it{italics}$', fontsize=14)
ax.text(-0.1, -0.5, 'nice italic font', style='italic', fontsize=14)
ax.set_xlabel('Nice$_2$ font', fontsize=14)
ax.set_ylabel('$Nice^+$ font', fontsize=14)
ax.tick_params(labelsize=12)
plt.show()

enter image description here

As it can be seen in the figure, the title has a mix between regular and italic fonts. How can I mix both types of font?

Upvotes: 1

Views: 1370

Answers (1)

pazitos10
pazitos10

Reputation: 1709

Make sure the font you choose has an italic style, otherwise matplotlib will use a fallback font for that.

For example:

# this will change regular text in the figure
plt.rcParams['font.family'] = 'Typewriter Revo'

# this is targeted to latex content
plt.rcParams['mathtext.it']= 'Typewriter Revo:italic'
plt.rcParams['mathtext.cal']= 'Typewriter Revo:italic'
plt.rcParams['mathtext.default'] = 'regular'
plt.rcParams["mathtext.fontset"] = 'custom'

plot with custom font

To be able to work with custom fonts I had to add them to matplotlib's list. Here is a good solution for that problem.

Upvotes: 1

Related Questions