Matplotlib rcParams not recognizing “Times New Roman” (mac High Sierra)

左心房为你撑大大i 提交于 2020-07-06 04:03:13

问题


I am trying to set the font of a matplotlib plot to Times New Roman. I have tried:

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'

I believe this is the correct way to set the font, but I keep getting this error that the found is not found:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/font_manager.py:1238: UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.

After some searching, I verified that I have the font downloaded. I tried opening a python shell and checking the contents of rcParams myself. Among a bunch of other font parameters, I curiously got a list of fonts in font.serif that contains Times New Roman.

          'font.serif': ['DejaVu Serif',
                         'Bitstream Vera Serif',
                         'Computer Modern Roman',
                         'New Century Schoolbook',
                         'Century Schoolbook L',
                         'Utopia',
                         'ITC Bookman',
                         'Bookman',
                         'Nimbus Roman No9 L',
                         'Times New Roman',
                         'Times',
                         'Palatino',
                         'Charter',
                         'serif'],

However, font.family only contained one item: sans-serif, when the matplotlib documentation states that there should be five values inside font.family. Has anybody run into this error before? How did you fix it?


回答1:


However, font.family only contained one item: sans-serif, when the matplotlib documentation states that there should be five values inside font.family

No, font.family should contain one of those five values, namely the font family that you want to use.

You gotta make sure that the font family is set to serif and the font Times New Roman is at the top of the serif font list. It works like this:

plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']

You technically don't need to append the rest of the list (the very last part). It simply provides a fallback for matplotlib if Times New Roman is actually not available.

Also see the example in the docs.



来源:https://stackoverflow.com/questions/52673444/matplotlib-rcparams-not-recognizing-times-new-roman-mac-high-sierra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!