我知道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字体?
发布评论