在 matplotlib 中更改字体

新手上路,请多包涵

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

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

按照 这个问题 的建议尝试:

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

>>>

这个答案 对我也没有太大帮助:

 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()

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

仍然不是时代

第一次尝试给出错误日志对我来说也有点奇怪,因为按照 这个答案 的建议表明 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'}

那么…我还能尝试什么?

原文由 A.Aussage 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 891
2 个回答

为了查看哪些字体可用,您应该使用 此方法

 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"

原文由 ImportanceOfBeingErnest 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

所有专有的 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 论文图终于开始工作了 :) 现在我可以继续 我在 LINUX 上的学术工作 <3

原文由 DrAcid 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题