Python 更改matplotlib中的字体

Python 更改matplotlib中的字体,python,python-3.x,matplotlib,fonts,Python,Python 3.x,Matplotlib,Fonts,所以我已经尝试了几乎所有我能在stackoverflow上找到的东西(以及谷歌能带我去的任何地方);我就是不能改变那该死的字体 下面是我迄今为止尝试过的非详尽列表: 按照以下建议进行尝试: 所以。。。我还可以尝试什么?要查看可用的字体,您应该使用: 然后看看是否有“新罗马时代” if "Times New Roman" in names: print "Yes" else: print "font not available" 如果不是,你就不能使用它。如果是,以下操作将按预期

所以我已经尝试了几乎所有我能在stackoverflow上找到的东西(以及谷歌能带我去的任何地方);我就是不能改变那该死的字体

下面是我迄今为止尝试过的非详尽列表:

按照以下建议进行尝试:


所以。。。我还可以尝试什么?

要查看可用的字体,您应该使用:

然后看看是否有“新罗马时代”

if "Times New Roman" in names:
    print "Yes"
else:
    print "font not available"
如果不是,你就不能使用它。如果是,以下操作将按预期工作

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

plt.plot([1,2,3])

plt.title('Please be Times New Roman')

plt.show()
请注意,您可以指定几种字体。将选择第一个实际可用的。在列表中最后添加“serif”至少可以确保在没有其他字体的情况下返回到serif字体

plt.rcParams["font.family"] = "Gulasch", "Times", "Times New Roman", "serif"

我想用同样的警告来解决同样的问题:

UserWarning:findfont:Font-family['Times New Roman']未找到。退回到DejaVu SAN

我读了很多关于堆栈溢出的帖子,直到我发现

现在我可以在
matplotlib

在我的情况下,我不得不使用
sudo
,因为我没有使用虚拟环境,所以我想这样做:

sudo cp/usr/share/font/truetype/msttcorefonts/Times\u New\u Roman*/usr/local/lib/python2.7/dist packages/matplotlib/mpl data/font/ttf

另一件我想查查的是该帖子的最后一条建议,即

注意:如果字体未呈现,可能是因为matplotlib需要刷新其字体缓存(请参见Kevin的评论):

import matplotlib

matplotlib.font\u管理器。\u rebuild()

这对我不起作用。相反,我使用了:

将matplotlib.font\u管理器作为fm导入


fm.\u rebuild()

我使用了这个Ipython代码来确保
Georgia
安装在一个virtualenv中。显然,
matplotlib.font\u管理器
找到它是不够的

import os.path
import matplotlib
import matplotlib.font_manager

destination = os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/fonts/ttf')
for font_filename in matplotlib.font_manager.get_fontconfig_fonts():
    try:
        if matplotlib.font_manager.FontProperties(fname=fname).get_name() == 'Georgia':
            !cp '{font_filename}' '{destination}'
            matplotlib.font_manager.rebuild()
            print('Installed ' font_filename)
            break
    except:
        pass


你试过其他字体吗?无论如何,您的最后一段代码似乎表明,您应该使用的字体名称是
'Times'
,而不是
'Times New Roman'
。我已经尝试了这两种字体,即使是'Times Roman',它仍然不起作用..似乎没有'Times New Roman';我是否需要按照中的建议强制添加它,或者是否有更简单的方法?您链接到的问题很好。您需要检查某处是否确实存在Times New Roman字体文件,如果不将其添加到目录中,请清空字体缓存。可以。嗯,似乎有一种STIX字体,看起来很像泰晤士报;我想使用已经包含的东西更安全!
import  matplotlib.font_manager
flist = matplotlib.font_manager.get_fontconfig_fonts()
names = [matplotlib.font_manager.FontProperties(fname=fname).get_name() for fname in flist]
print names
if "Times New Roman" in names:
    print "Yes"
else:
    print "font not available"
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "Times New Roman"

plt.plot([1,2,3])

plt.title('Please be Times New Roman')

plt.show()
plt.rcParams["font.family"] = "Gulasch", "Times", "Times New Roman", "serif"
import os.path
import matplotlib
import matplotlib.font_manager

destination = os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/fonts/ttf')
for font_filename in matplotlib.font_manager.get_fontconfig_fonts():
    try:
        if matplotlib.font_manager.FontProperties(fname=fname).get_name() == 'Georgia':
            !cp '{font_filename}' '{destination}'
            matplotlib.font_manager.rebuild()
            print('Installed ' font_filename)
            break
    except:
        pass