Matplotlib:轴标签中的不同字体类型渲染

时间:2013-10-09 15:58:29

标签: python matplotlib font-face axis-labels

使用下面的代码,我得到一个带有两种不同字体类型的标签。 “表面区域”是无衬线,而Angstrom和上标是衬线。无论如何我可以制作整个y-label sans-serif?

提前致谢!

import matplotlib.pylab as plt
import random

random.seed(20)
data = [random.random() for i in range(10)]

fig, ax = plt.subplots()
ax.plot(data)
ax.set_ylabel(r'Surface Area ($\AA^2$)', size =16)

plt.show()

Surface Area

1 个答案:

答案 0 :(得分:1)

matplotlib版本1.3.0及更高版本中的快速而肮脏的方式,您可以使用Latex \textup\textbf

ax.set_ylabel('\textup{Surface Area} (\AA^2)', size =16)

请参阅此处的教程:http://matplotlib.org/users/usetex.html,不要忘记通过以下方式启用latex

rc('text', usetex=True)

这适用于旧版本以及安装Latex的版本。但如果你想坚持matplotlib的{​​{1}},也可以这样做:

mathtext

并非所有字体都适用于>>> matplotlib.rcParams['mathtext.fontset']='stixsans' >>> for i in [1]: fig, ax = plt.subplots() ax.plot(data) ytx=ax.set_ylabel(r'Surface Area $(\AA^2)$', size =16) mathtext是“看起来像'stixsanshttp://matplotlib.org/users/mathtext.html#mathtext-tutorial的字体。老实说,差异非常小。我非常确定Angstrom不在sans-serif中,所以要呈现它stixsans会回到默认的mathtext字体。

enter image description here