python中rcparams_如何使用mpl.rcParams在matplotlib中加载.ttf文件?

1586010002-jmsa.png

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

引用\[1\]和\[2\]提供了关于报错的原因和解决办法。报错的原因是Anaconda3的字体没有文字simhei.ttf。解决办法是下载simhei.ttf字体,并将其放入解释器的fonts文件夹下的ttf文件。具体的文件夹路径取决于你的解释器的安装位置。如果你使用的是Anaconda解释器,路径可能是D:\AnaInstallDir\Anaconda3\Lib\site-packages\matplotlib\mpl-data\fonts\ttf。如果你使用的是自己安装的Python解释器,路径一般为C:\Users\用户名\AppData\Local\Programs\Python\Python35\Lib\site-packages\matplotlib\mpl-data\fonts\ttf。将下载的SimHei.ttf文件放入ttf文件夹后,再运行程序就可以成功运行代码plt.rcParams\['font.sans-serif'\] = \['SimHei'\]了。\[1\]\[2\] #### 引用[.reference_title] - *1* [python用来正常显示文标签 plt.rcParams['font.sans-serif'] = ['SimHei']错误](https://blog.csdn.net/xcjJenifer/article/details/105419979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python用来正常显示文标签 plt.rcParams[‘font.sans-serif‘] = [‘SimHei‘]错误](https://blog.csdn.net/qq_44421796/article/details/116200952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [用来正常显示文标签显示错误代码plt.rcParams[‘font.sans-serif’] = [‘SimHei’]无法运行](https://blog.csdn.net/qq_42522007/article/details/112259845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值