Matplotlib:Times New Roman看起来很大胆

Ced*_* H. 9 python matplotlib

出于某种原因,在我的mpl情节中使用Times New Roman时,它看起来很大胆.其他字体都可以.

这是一个最小的例子,结果(在Word文档中,与我期望的Times New Roman看起来相似).

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')
Run Code Online (Sandbox Code Playgroud)

包含word样式表

backend: PS
text.usetex: False
font.family: serif
font.serif: Times New Roman
font.size: 11
axes.titlesize: 11
axes.labelsize: 11
Run Code Online (Sandbox Code Playgroud)

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

正确找到了字体,它也在数学模式下工作(参见图中的alpha).看来这很大胆......

在此输入图像描述

den*_*nis 13

我知道这个问题已经很老了,但它仍然是一个问题,至少在我的Mac上是这样.我找到了一个非常简单的解决方案,由azag0在github上发布

del matplotlib.font_manager.weight_dict['roman']
matplotlib.font_manager._rebuild()
Run Code Online (Sandbox Code Playgroud)

https://github.com/matplotlib/matplotlib/issues/5574


Ced*_* H. 7

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

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

<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
Run Code Online (Sandbox Code Playgroud)

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

 if score < best_score:
     best_score = score
     best_font = font
Run Code Online (Sandbox Code Playgroud)

我肮脏的补丁来代替<<=...

  • 近 5 年后,这个错误仍然存​​在,而你的肮脏修复仍然解决了问题:) 谢谢! (3认同)