matplotlib mathtext找不到字体

时间:2013-03-01 00:04:05

标签: python fonts matplotlib

我正在试图弄清楚如何在python matplotlib中使用mathtext。

他们提供了一些我在下面复制的small example code

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
     fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.show()

当我尝试运行此代码时,我收到许多警告,如下所示: /usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1242: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))

并且mathtext无法正确呈现。这个数字看起来像这样:

Figure with broken fonts

什么时候看起来像这样:

how figure should look

1 个答案:

答案 0 :(得分:0)

显然这是一个旧版matplotlib的错误,这是我的fedora存储库中的版本。我下载并安装了最新版本,这解决了这个问题。

要获得最新版本,我必须从源代码构建它。按照matplotlib installation instructions page的说明进行操作,

首先,获取构建依赖项: sudo yum-builddep python-matplotlib

然后,从download page获取最新的.tar.gz(撰写本文时为1.2版)

然后,其余部分由python安装脚本处理。

cd matplotlib // wherever the .tar.gz was extracted to.
python setup.py build
python setup.py install