我想创建一个带有x轴标签的matplotlib图形,该标签是Arial字体,并且有一个单词斜体。
我可以使用乳胶字体创建带有x轴标签的图形,并用一个单词斜体显示;我也可以在Arial中创建带有x轴标签的图形,只要我有整个标签斜体或整个标签正常;但我无法在Arial斜体中获得xlabel的一部分,而另一部分则在arial斜体中。
我主要尝试以下代码的排列:
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')
答案 0 :(得分:2)
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()