Matplotlib:Times New Roman出现为粗体 [英] Matplotlib: Times New Roman appears bold

查看:97
本文介绍了Matplotlib:Times New Roman出现为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,在我的 mpl 图中使用 Times New Roman 时,它显得粗体.其他字体都可以.

For some reason when using Times New Roman in my mpl plots it appears bold. Other fonts are OK.

这是一个最小的示例,其结果(在Word文档中,用于与Times New Roman的外观进行比较).

Here is a minimal example, and the result (inside a Word document, for comparison with what I expect Times New Roman to look like).

import matplotlib as mpl
import matplotlib.pyplot as plt

with plt.style.context('word'):
  fig = plt.figure(1, figsize=(3.4, 2.1))
  ax1 = plt.subplot(111)
  ax1.plot([1,2,3,4,5], '+--')
  ax1.text(0.5, 3.5, r"Brown $\alpha + 12 \sum_ix$")
  ax1.text(0.5, 3, r"1.0 2.0")
  ax1.set_xlabel('normal 1.0 and math $1.0$')
  ax1.set_ylabel('Times New Roman')
  plt.tight_layout()
  fig.savefig('word.pdf')

带有 word 样式表包含

backend: PS
text.usetex: False
font.family: serif
font.serif: Times New Roman
font.size: 11
axes.titlesize: 11
axes.labelsize: 11

该图以其实际大小(3.4'' x 2.1'')包含在文档中.

The plot is included in the document with its actual size (3.4'' by 2.1'').

可以正确找到该字体,并且它也可以在数学模式下工作(请参见图中的字母).看来这是大胆的...

The font is correctly found and it is also working in math-mode (see the alpha in the plot). It just seems that this is bold...

推荐答案

深入研究更多细节,我意识到这个错误是真实存在的,mpl 实际上是在选择 Times New Roman Bold 字体.

Digging into more details I realized that the bug is real and that mpl is actually selecting a Times New Roman Bold font.

font_manger.py 中的字体选择算法会根据字体,变体,粗细等为找到的每种字体分配权重(大约1290行).来自 Times New Roman Bold.ttf 的名称"只是"Times New Roman",可能有意义,但是粗细为500,与常规字体的值相同:

The font selection algorithm in font_manger.py assigns weights on every font it finds based on the family, variant, weight, etc. (around line 1290). The "name" coming from Times New Roman Bold.ttf is just 'Times New Roman' which might make sense, but the weight is 500, the same value as the regular font:

<Font 'Times New Roman' (Times New Roman Bold.ttf) normal normal 500 normal> with score 0.1
<Font 'Times New Roman' (Times New Roman.ttf) normal normal 500 normal> with score 0.1

在我的Mac和Linux设置上,首先遇到粗体,并由代码选择

On my Mac and Linux setup the bold one is encountered first and is selected by the code

 if score < best_score:
     best_score = score
     best_font = font

我的脏补丁是将 < 替换为 <=...

I dirty patch is to replace < by <=...

这篇关于Matplotlib:Times New Roman出现为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆