Matplotlib rcParams无法识别"Times New Roman" (mac High Sierra) [英] Matplotlib rcParams not recognizing "Times New Roman" (mac High Sierra)

查看:614
本文介绍了Matplotlib rcParams无法识别"Times New Roman" (mac High Sierra)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将matplotlib图的字体设置为Times New Roman.我已经尝试过:

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.

经过一些搜索,我确认我已经下载了字体.我尝试打开python shell并自己检查rcParams的内容.在许多其他字体参数中,我很好奇地在font.serif中找到了包含Times New Roman的字体列表.

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'],

但是,当 matplotlib文档说明有内容时,font.family仅包含一项:sans-serif. a>在font.family中应该有五个值.有人遇到过这个错误吗?您是如何解决的?

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?

推荐答案

但是,font.family仅包含一项:sans-serif,当 matplotlib文档指出应该有五个值 内部font.family

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

否,font.family应该包含这五个值之一,即您要使用的字体系列.

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

您必须确保将字体系列设置为 serif ,并且字体 Times New Roman 位于衬线字体列表的顶部.它是这样的:

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']

从技术上讲,您无需附加列表的其余部分(最后一部分).如果实际上没有Times New Roman,它只是为matplotlib提供了一个后备.

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.

另请参阅文档中的示例.

这篇关于Matplotlib rcParams无法识别"Times New Roman" (mac High Sierra)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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