简体   繁体   English

如何将unicode字体加载到matplotlib中?

[英]How can I load a unicode font into matplotlib?

I'm trying to plot unicode symbols. 我正在尝试绘制unicode符号。 I've tried a variety of approaches and always get stuck with bitstream vera sans, which doesn't have the symbols I need. 我尝试了多种方法,并始终陷入比特流的困境,而比特流没有我需要的符号。

I have unsuccessfully tried deleting the font cache ( ~/.matplotlib/fontList.*cache ), loading the font directly from file (eg How to use a (random) *.otf or *.ttf font in matplotlib? ), and some other SO suggestions that I've now lost. 我尝试删除字体缓存( ~/.matplotlib/fontList.*cache ),直接从文件中加载字体(例如, 如何在matplotlib中使用(随机)*。otf或* .ttf字体 ),但还是失败了 ,还有一些我现在已经失去的其他建议。

>>> import matplotlib
>>> matplotlib.use('agg')
>>> import matplotlib.font_manager as font_manager
>>> prop = font_manager.FontProperties('/Library/Fonts/Arial Unicode.ttf')
>>> prop.get_name()
/Users/adam/anaconda/envs/astropy27/lib/python2.7/site-packages/matplotlib/font_manager.py:1279: UserWarning: findfont: Font family ['/Library/Fonts/Arial Unicode.ttf'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'Bitstream Vera Sans'

How can I load this font, or at least something nearly equivalent? 如何加载此字体,或者至少加载几乎等同的字体?

This could work, it did at least for me: 这可能有效,至少对我有用:

import matplotlib as mpl
import matplotlib.font_manager as font_manager

path_font = '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/cmunrm.ttf'
prop = font_manager.FontProperties(fname=path_font)
#print prop.get_name()
mpl.rcParams['font.family'] = prop.get_name()
mpl.rcParams['font.size'] = 16.
mpl.rcParams['axes.labelsize'] = 12.
mpl.rcParams['xtick.labelsize'] = 12.
mpl.rcParams['ytick.labelsize'] = 12.

The problem is in line 问题出在

>>> prop = font_manager.FontProperties('/Library/Fonts/Arial Unicode.ttf')

You're setting the first argument, family, but you want to set the file name, fname. 您要设置第一个参数family,但是要设置文件名fname。 So this instead: 所以这代替:

>>> prop = font_manager.FontProperties(fname='/Library/Fonts/Arial Unicode.ttf')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM