Reputation: 2164
I want to create a matplotlib figure with an x-axis label that is in Arial font, and has one word italicised.
I can create figures with x-axis labels in latex-font with one word italicised; I can also create figures with x-axis labels in Arial, as long as I have either the entire label italicised or the entire label normal; but I can't get part of the xlabel in Arial italicised and the other part in arial normal.
I'm mostly trying permutations of the following code:
from matplotlib import pyplot as plt
import numpy as n
import matplotlib
from matplotlib.font_manager import FontProperties
font = {'family' : 'Arial',
'weight' : 'medium',
'size' : 20,
'style' : 'normal'}
font0 = {'family' : 'Arial',
'weight' : 'medium',
'size' : 20,
'style' : 'italic'}
matplotlib.rcParams['mathtext.fontset'] = 'custom'
matplotlib.rcParams['mathtext.rm'] = 'Arial'
matplotlib.rcParams['mathtext.it'] = 'Arial'
matplotlib.rc('font', **font)
#matplotlib.rc('font', **font0)
matplotlib.rc('text', usetex=False)
plt.figure(); plt.plot(n.linspace(0,3,10), n.linspace(0,3,10))
plt.xlabel(r'$\mathit{italics}$ - rest normal')
Upvotes: 5
Views: 14187
Reputation: 16249
from matplotlib.pyplot import *
# Need to use precise font names in rcParams; I found my fonts with
#>>> import matplotlib.font_manager
#>>> [f.name for f in matplotlib.font_manager.fontManager.ttflist]
rcParams['mathtext.fontset'] = 'custom'
rcParams['mathtext.it'] = 'Arial:italic'
rcParams['mathtext.rm'] = 'Arial'
fig, ay = subplots()
# Using the specialized math font elsewhere, plus a different font
xlabel(r"$\mathit{Italic}$ $\mathrm{and\ just\ Arial}$ and not-math-font",fontsize=18)
# No math formatting, for comparison
ylabel(r'Italic and just Arial and not-math-font', fontsize=18)
grid()
show()
Upvotes: 5