Matplotlib'粗体'字体

时间:2013-09-15 12:57:52

标签: python matplotlib

关注this example

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
for i, label in enumerate(('A', 'B', 'C', 'D')):
    ax = fig.add_subplot(2,2,i+1)
    ax.text(0.05, 0.95, label, transform=ax.transAxes,
      fontsize=16, fontweight='bold', va='top')

plt.show()

我得到了这个输出:

enter image description here

为什么我的标签正常重量,而文档显示这应该创建粗体字母ABCD

我也收到了这个警告:

Warning (from warnings module):
File "C:\Python27\lib\site-packages\matplotlib\font_manager.py", line 1228
UserWarning)
UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=italic:variant=normal:weight=bold:stretch=normal:size=x-small. Returning C:\Python27\lib\site-packages\matplotlib\mpl-data\fonts\ttf\Vera.ttf

5 个答案:

答案 0 :(得分:7)

尝试使用weight代替fontweight

答案 1 :(得分:5)

您问题中的示例适用于我的机器。因此,您肯定有一个库问题。您是否考虑过使用乳胶制作粗体文字?这是一个例子

enter image description here

代码

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots(3, 1)
ax0, ax1, ax2 = axs

ax0.text(0.05, 0.95, 'example from question',
        transform=ax0.transAxes, fontsize=16, fontweight='bold', va='top')
ax1.text(0.05, 0.8, 'you can try \\textbf{this} using \\LaTeX', usetex=True,
        transform=ax1.transAxes, fontsize=16, va='top')
ax2.text(0.05, 0.95,
         'or $\\bf{this}$ (latex math mode with things like '
         '$x_\mathrm{test}^2$)',
        transform=ax2.transAxes, fontsize=10, va='top')

plt.show()

答案 2 :(得分:2)

也许尝试使用这个 -

plt.rcParams['axes.labelsize'] = 16
plt.rcParams['axes.labelweight'] = 'bold'

在您的计划中在全球范围内执行此操作。

答案 3 :(得分:1)

不确定您是否仍然遇到此问题。我在Anaconda / Spyder,Python 2.7中尝试了你的代码。图中显示粗体标签(A,B,C,D)。我同意这个问题可能与图书馆有关。尝试替换/更新font_manager.py或确认存在字体文件:

LIB \站点包\ matplotlib \ MPL-数据\字体\ TTF \

答案 4 :(得分:0)

我遇到了同样的问题,今天花了几个小时。这是对我有帮助的解决方案:

import matplotlib
matplotlib.font_manager._rebuild()

这样,font_manager可以轻松升级。