Python Matplotlib“粗体”字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18812646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:56:26  来源:igfitidea点击:

Matplotlib 'bold' font

pythonmatplotlib

提问by user1915817

Following 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()

I get this output:

我得到这个输出:

enter image description here

在此处输入图片说明

Why are my labels normal weight, while the documentation shows this should create bold letters A, B, C, D?

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

I also get this warning:

我也收到此警告:

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

回答by kibitzforu

Not sure if you're still having the issue. I tried your code in Anaconda/Spyder, Python 2.7. The plots appear with Bold labels (A,B,C,D). I agree the issue is probably with the library. Try replacing / updating font_manager.py or confirming font files are present:

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

Lib\site-packages\matplotlib\mpl-data\fonts\ttf\

Lib\site-packages\matplotlib\mpl-data\fonts\ttf\

回答by Dinesh

Maybe try using this -

也许尝试使用这个 -

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

Do this at a global level in your program.

在您的程序中在全局级别执行此操作。

回答by Safwan

Try using weightinstead of fontweight.

尝试使用weight代替fontweight.

回答by Markus Dutschke

The example from your question works on my machine. Hence you definately have a library problem. Have you considered using latex to make bold text? Here an example

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

enter image description here

在此处输入图片说明

Code

代码

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()