【问题标题】:Changing fonts in matplotlib在 matplotlib 中更改字体
【发布时间】:2018-02-03 06:55:12
【问题描述】:

所以我已经尝试了几乎所有我可以在 stackoverflow 上找到的东西(以及谷歌会引导我的任何其他地方);我就是不能改变该死的字体!

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

按照this question 中的建议尝试:

import matplotlib.pyplot as plt
csfont = {'fontname':'Times New Roman'}
x = [1,2,3]
y = x

plt.plot(x,y)

plt.title('Please be Times >__<',**csfont)

plt.show()

给我这个错误日志:

>>> (executing file "<tmp 1>")
Note on using QApplication.exec_():
The GUI event loop is already running in the pyzo kernel, and exec_()
does not block. In most cases your app should run fine without the need
for modifications. For clarity, this is what the pyzo kernel does:
- Prevent deletion of objects in the local scope of functions leading to exec_()
- Prevent system exit right after the exec_() call
/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

>>> 

This answer 也没有帮到我:

import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "Times New Roman"
x = [1,2,3]
y = x

plt.plot(x,y)

plt.title('Please be Times >__<',**csfont)

plt.show()

没有显示错误;但这不是时代……

第一次尝试给出错误日志对我来说也有点奇怪,因为按照this answer 的建议进行操作表明 Times New Roman 确实是我应该能够使用的字体:

>>> set([f.name for f in matplotlib.font_manager.fontManager.afmlist])
{'Courier', 'Times', 'URW Bookman L', 'Nimbus Mono L', 'ITC Bookman', 'ZapfDingbats', 'Century Schoolbook L', 'New Century Schoolbook', 'Helvetica', 'Standard Symbols L', 'Utopia', 'Palatino', 'URW Gothic L', 'Courier 10 Pitch', 'Symbol', 'Computer Modern', 'Bitstream Charter', 'ITC Avant Garde Gothic', 'Nimbus Roman No9 L', 'ITC Zapf Chancery', 'ITC Zapf Dingbats', 'URW Chancery L', 'Nimbus Sans L', 'Dingbats', 'URW Palladio L'}

那么...我还能尝试什么?

【问题讨论】:

  • 你试过其他字体吗?无论如何,您最后的 sn-p 似乎表明您应该使用的字体名称可能是 'Times' 而不是 'Times New Roman'
  • 我都试过了,即使是'Times-Roman',它仍然没有工作..

标签: python python-3.x matplotlib fonts


【解决方案1】:

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

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

然后看看有没有“Times New Roman”。

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" 至少可以确保在没有其他字体存在时回退到衬线字体。

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

【讨论】:

  • 似乎没有“Times new Roman”;我需要按照this question 中的建议强制添加它还是有更简单的方法?
  • 您链接到的问题很好。您需要检查某处是否确实存在 Times New Roman 字体文件,如果没有将其添加到目录中,请清空字体缓存。可以工作。
  • 嗯,似乎有一种 STIX 字体看起来与 times 非常相似;我想只使用已经包含的东西会更安全!
【解决方案2】:

同样的问题,同样的警告:

UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans

在找到 this. 之前,我阅读了很多 Stack Overflow 帖子都无济于事

现在我可以在matplotlib 中使用Times New Roman

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

sudo cp /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman* /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf

我要查找的另一件事是帖子的最后一条建议,即,

注意:如果字体没有渲染,可能是因为matplotlib需要刷新它的字体缓存(见Kevin的评论):

import matplotlib

matplotlib.font_manager._rebuild()

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

import matplotlib.font_manager as fm

fm._rebuild()

【讨论】:

    【解决方案3】:

    我使用这个 ipython 代码来确保 Georgia 已安装在 virtualenv 中。显然matplotlib.font_manager 找不到它是不够的。

    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
    

    【讨论】:

      【解决方案4】:

      我在 Python 3.9 上遇到过同样的问题,Anaconda 在 Pop!_OS 20.04 Linux 上遇到过同样的问题。

      所有专有的 MS 字体均使用 sudo apt install ttf-mscorefonts-installer,但完全没有帮助。

      为具有 MS 字体的 CONDA 安装包也没有帮助matplotlib! :(

      另外,我的fontmanager 抱怨没有重建这样的东西!

      AttributeError: module 'matplotlib.font_manager' has no attribute '__rebuild'

      解决方案:

      我已将 MS 字体从 /home/dracid/anaconda3/fonts 复制到 Matplotlib 的 OWN 字体文件夹。在我的系统上是:

      /home/dracid/anaconda3/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf

      之后,需要删除matplotlib字体/home/dracid/.cache/matplotlib/fontlist-v330.json,然后在下一次启动Python会话时,它会重建缓存。

      那么,VIOLA 我的 IEEE 论文情节终于开始工作了 :) 现在我可以继续 with my academic work on LINUX <3

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-22
        相关资源
        最近更新 更多