如何使用mpl.rcParams在matplotlib中加载.ttf文件? [英] How to load .ttf file in matplotlib using mpl.rcParams?
问题描述
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
mpl.rcParams ['xtick.labelsize'] = 16
...
我已经使用了命令
$ p $ fm.findSystemFonts ()
来获取我的系统上的字体列表。我发现了一个.ttf文件的完整路径,我想使用,
$ p $ '/ usr / share / fonts / truetype / anonymous-pro / Anonymous Pro BI.ttf'
我试过使用这个字体
mpl.rcParams ['font.family'] ='anonymous-pro'
和
mpl.rcParams ['font.family'] ='匿名Pro BI'
/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1218:UserWarning:findfont:Font family [ 'anonymous-pro']找不到。回到Bitstream Vera Sans
我可以使用mpl.rcParams字典在我的图中设置这个字体?
编辑
在阅读了更多内容之后,看起来这是确定字体名称的一般问题从一个.ttf文件。这是很容易在Linux或Python?
另外,我试过添加
mpl.use ['agg']
mpl.rcParams ['text.usetex'] = False
没有任何成功
指定一个字体系列:
如果您所知道的是ttf的路径,那么您可以使用 get_name
方法发现字体系列名称:
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf'
prop = font_manager.FontProperties(fname = path)
mpl.rcParams ['font.family'] = prop.get_name()
fig,ax = plt.subplots()
ax.set_title('Text in a cool font',size = 40)
plt.show()
$ b按路径指定字体:
import matplot lib.pyplot plt
import matplotlib.font_manager as font_manager
$ b $ path ='/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf'
prop = font_manager.FontProperties (fname = path)
fig,ax = plt.subplots()
ax.set_title('cool in font font',fontproperties = prop,size = 40)
plt.show )
I have a matplotlib script that starts ...
import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.font_manager as fm mpl.rcParams['xtick.labelsize']=16 ...
I've used the command
fm.findSystemFonts()
to get a list of the fonts on my system. I've discovered the full path to a .ttf file I'd like to use,
'/usr/share/fonts/truetype/anonymous-pro/Anonymous Pro BI.ttf'
I've tried to use this font without success using the following commands
mpl.rcParams['font.family'] = 'anonymous-pro'
and
mpl.rcParams['font.family'] = 'Anonymous Pro BI'
which both return something like
/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1218: UserWarning: findfont: Font family ['anonymous-pro'] not found. Falling back to Bitstream Vera Sans
Can I use the mpl.rcParams dictionary to set this font in my plots?
EDIT
After reading a bit more, it seems this is a general problem of determining the font family name from a .ttf file. Is this easy to do in linux or python ?
In addition, I've tried adding
mpl.use['agg'] mpl.rcParams['text.usetex'] = False
without any success
解决方案Specifying a font family:
If all you know is the path to the ttf, then you can discover the font family name using the
get_name
method:import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf' prop = font_manager.FontProperties(fname=path) mpl.rcParams['font.family'] = prop.get_name() fig, ax = plt.subplots() ax.set_title('Text in a cool font', size=40) plt.show()
Specifying a font by path:
import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf' prop = font_manager.FontProperties(fname=path) fig, ax = plt.subplots() ax.set_title('Text in a cool font', fontproperties=prop, size=40) plt.show()
这篇关于如何使用mpl.rcParams在matplotlib中加载.ttf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!