Matplotlib: Times New Roman appears bold

后端 未结 8 1748
醉梦人生
醉梦人生 2021-02-14 23:40

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

Here is a minimal example, and the result (inside a Word doc

相关标签:
8条回答
  • 2021-02-14 23:53

    Using python 3.7 on Windows 10, Jupyter Notebook, no Anaconda installed.

    Simply adjusted the font sequence C:\Windows\Fonts\Times New Roman as suggested above doesn't work here.

    Under C:\User\Name\.matplolib, find a JSON File (text file) named fontlist-v300.

    Delete

    {
          "fname": "C:\\WINDOWS\\Fonts\\timesbd.ttf",
          "name": "Times New Roman",
          "style": "normal",
          "variant": "normal",
          "weight": "roman",
          "stretch": "normal",
          "size": "scalable",
          "__class__": "FontEntry"
    }
    

    in the text file works for me.

    Not a good way, though.

    0 讨论(0)
  • 2021-02-14 23:54

    I know the question is very old, but it still is a problem, at least for me on my mac. I found a very easy solution to this problem, posted by azag0 on github

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

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

    0 讨论(0)
  • 2021-02-15 00:04

    As mentioned the font selection algorithm picks the first font in the Time New Roman family, which has four different files (bold, bold italic, italic and regular). So the steps would be:

    1. Go to C:\Windows\Fonts\Times New Roman
    2. Right-click → Sort by → Descending (the order will change so the Times New Roman Regular will end up as the first, and that will be the font the algorithm will choose).
    0 讨论(0)
  • 2021-02-15 00:10

    I solved this problem by deleting the timesbd.tff in matplotlib/mpl-data/fonts. And I'm not sure whether timesbi.tff and timesi.tff need to be deleted, but I did it.

    0 讨论(0)
  • 2021-02-15 00:11

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

    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
    

    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 <=...

    0 讨论(0)
  • 2021-02-15 00:15

    As stated above the default font weighting is high for Times New Roman font in mpl (claim was 500 above, which I will take their word for it).

    The easy fix for this is to set the weighting yourself using fontweight.

    plt.xlabel('t',fontsize=32, fontname = 'Times New Roman', fontweight = 250)
    

    This worked for me and looked normal.

    0 讨论(0)
提交回复
热议问题