python用哪种字体_找出matplotlib使用哪种字体

Im looking for a nice way to get the name of the default font that is used by matplotlib.pyplot. The documentation indicates that the font is selected from the list in rcParams['font.family'] which is ordered top down by priority.

My first try was to check for warnings, i.e.,

import matplotlib.pyplot as plt

import warnings

for font in plt.rcParams['font.sans-serif']:

print font

with warnings.catch_warnings(record=True) as w:

warnings.simplefilter("always")

plt.rcParams['font.family'] = font

plt.text(0,0,font)

plt.savefig(font+'.png')

if len(w):

print "Font {} not found".format(font)

which gives me

Bitstream Vera Sans

Font Bitstream Vera Sans not found

DejaVu Sans

Lucida Grande

Font Lucida Grande not found

Verdana

Geneva

Font Geneva not found

Lucid

Font Lucid not found

Arial

Helvetica

Font Helvetica not found

Avant Garde

Font Avant Garde not found

sans-serif

I can tell that on this machine, DejaVu Sans is used by matplotlib.pyplot.

However, I thought there should be an easier way to obtain this information.

Edit:

The warning can be triggered directly via

matplotlib.font_manager.findfont(matplotlib.font_manager.FontProperties(family=font))

解决方案

To obtain the font family:

matplotlib.rcParams['font.family']

If it's a general font family like 'sans-serif', use fontfind to find the actual font:

>>> from matplotlib.font_manager import findfont, FontProperties

>>> font = findfont(FontProperties(family=['sans-serif']))

>>> font

'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/mpl-data/fonts/ttf/Vera.ttf'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值