2015-11-27 114 views
2

由於某些原因,在我的mpl圖中使用Times New Roman時,它顯示爲粗體。其他字體都可以。Matplotlib: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') 

用含有

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

情節的word樣式表是包含在其實際尺寸(3.4 '' 2.1 '')在文檔中。

字體正確找到,它也在數學模式下工作(請參閱圖中的alpha)。它只是似乎這是大膽的...

enter image description here

回答

3

挖掘到更多的細節我意識到錯誤是真實的,mpl實際選擇宋體加粗字體。

font_manger.py中的字體選擇算法根據系列,變量,權重等(在1290行左右)爲其找到的每種字體分配權重。 「名稱」從Times New Roman Bold.ttf來只是「宋體」,這可能是有意義的,但重量是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 

在我的Mac和Linux安裝大膽的一個是第一次遇到,並通過代碼

if score < best_score: 
    best_score = score 
    best_font = font 

我骯髒的補丁是<=更換< ...

0

刪除~/.cache/matplotlib/解決了這個問題對我來說選擇。

2

我知道這個問題很舊,但它仍然是一個問題,至少對我來說,在我的Mac上。我發現了一個很容易解決這個問題,發表azag0在github

del matplotlib.font_manager.weight_dict['roman'] 
matplotlib.font_manager._rebuild() 

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

相關問題