为什么matplotlib不使用我提供的.ttf字体?

编程入门 行业动态 更新时间:2024-10-21 19:40:49
本文介绍了为什么matplotlib不使用我提供的.ttf字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在matplotlib中使用TTF字体; .ttf文件被下载并在我的机器上本地居住。我遵循本网站上的其他说明使用 font_manager 选择字体;但是,我尝试使用字体属性的任何文本仍然以默认的matplotlib字体显示。

我知道Python能成功找到字体文件,因为 prop.get_name()和类似命令显示我想要的字体的属性 - 但这不是我的数字上出现的。有什么建议?

举例:

import matplotlib.pyplot as plt import matplotlib.font_manager as fm fig,ax = plt.subplots() prop = fm.FontProperties(fname ='/ Users / smith $ b

解决方案

它是因为您正在使用的后端。

当我试着做类似于我的默认后端 MacOS 和 cairo 后端它没有工作。然而,当我切换到 agg 和 TKagg 以及运行您的示例自定义字体在那里。

这是您的代码修改,以便它在我的机器上运行

#!/ usr / bin / env python import matplotlib matplotlib.use(agg) import matplotlib.pyplot as plt import matplotlib.font_manager as fm fig,ax = plt.subplots() prop = fm.FontProperties(fname ='Outwrite.ttf') ax.set_title('Text in a cool font',fontproperties = prop,size = 40) plt.show() plt.savefig('test.png')

生成的图片是自定义字体。

I'm trying to use a TTF font in matplotlib; the .ttf file is downloaded and lives locally on my machine. I've followed other instructions on this site for selecting the font using font_manager; however, any text that I generate trying to use the font properties still appears in the default matplotlib font.

I know that Python does successfully find the font file, since prop.get_name() and similar commands do show the attributes of the font I want - but that's not what appears on my figure. Any suggestions?

As an example:

import matplotlib.pyplot as plt import matplotlib.font_manager as fm fig, ax = plt.subplots() prop = fm.FontProperties(fname='/Users/smith/fonts/coolfont.ttf') ax.set_title('Text in a cool font', fontproperties=prop, size=40) fig.show()

解决方案

its because of the backend that you are using.

When I tried to do something similar with my default backend which is MacOS and with the cairo backend it didnt work.

However when I switched to agg and TKagg and run your example the custom font was there.

Here is your code modified so that it runs on my machine

#!/usr/bin/env python import matplotlib matplotlib.use( "agg" ) import matplotlib.pyplot as plt import matplotlib.font_manager as fm fig, ax = plt.subplots() prop = fm.FontProperties(fname='Outwrite.ttf') ax.set_title('Text in a cool font', fontproperties=prop, size=40) plt.show() plt.savefig('test.png')

The resulting image is with a custom font.

更多推荐

为什么matplotlib不使用我提供的.ttf字体?

本文发布于:2023-10-30 04:27:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字体   matplotlib   ttf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!