为什么matplotlib不使用我提供的

为什么matplotlib不使用我提供的

本文介绍了为什么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 以及 $ b 运行您的示例自定义字体在那里。



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

 #!/ 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字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:57