本文主要是介绍Linux/MacOS下matplotlib能正常显示的中文字体选择,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
下面的Python脚本可以检测到 *nix 系统内 matplotlib 支持正常显示的中文字体(用到了命令行工具 fc-list ):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File Name: font_check.py
# Created Time: Thu Mar 23 16:53:59 2017__author__ = 'minyu'
__mail__ = 'minyu7374@gmail.com'from matplotlib.font_managerimport FontManager
import subprocessmpl_fonts = set(f.namefor f in FontManager().ttflist)print ('all font list get from matplotlib.font_manager:')
for f in sorted(mpl_fonts):print('\t' + f)# for python2
# output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
# for python3
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True, encoding="utf8")zh_fonts = set(f.split(',',1)[0] for f in output.split('\n'))print('\n' +'Chinese font list get from fc-list:')
for f in sorted(zh_fonts):print('\t' + f)print('\n' +'the fonts we can use:')
available = set(mpl_fonts) & set(zh_fonts)
for f in available:print('\t' + f)
在个人的 MacOS Sierra 系统下运行结果如下:
Fontconfig warning: ignoring UTF-8: not a valid region tag
all font list get from matplotlib.font_manager:.Keyboard.LastResort.SF Compact Display.SF Compact Rounded.SF Compact Text.SF NS Display Condensed.SF NS Text CondensedAndale MonoApple BrailleApple ChanceryApple SymbolsAppleGothicAppleMyungjoArialArial BlackArial NarrowArial Rounded MT BoldArial Unicode MSAyuthayaBig CaslonBitstream Vera SansBitstream Vera Sans MonoBitstream Vera SerifBodoni 72 SmallcapsBodoni OrnamentsBradley HandBrush Script MTChalkdusterComic Sans MSConsolasCourier NewDIN AlternateDIN CondensedDejaVu SansDejaVu Sans DisplayDejaVu Sans MonoDejaVu SerifDejaVu Serif DisplayDiwan ThuluthEast Syriac AdiabeneEast Syriac CtesiphonEstrangelo AntiochEstrangelo EdessaEstrangelo MidyatEstrangelo NisibinEstrangelo Nisibin OutlineEstrangelo QuenneshrinEstrangelo TaladaEstrangelo TurAbdinFarisiGeorgiaGoha-Tibeb ZemenGurmukhi MTHerculanumHoefler TextImpactInaiMathiKhmer Sangam MNKokonorKrungthepLao Sangam MNLuminariLuxi MonoLuxi SansLuxi SerifMicrosoft Sans SerifMicrosoft YaHei MonoMishafiMishafi GoldPlantagenet CherokeeSTIXGeneralSTIXIntegralsDSTIXIntegralsSmSTIXIntegralsUpSTIXIntegralsUpDSTIXIntegralsUpSmSTIXNonUnicodeSTIXSizeFiveSymSTIXSizeFourSymSTIXSizeOneSymSTIXSizeThreeSymSTIXSizeTwoSymSTIXVariantsSathuSerto BatnanSerto JerusalemSerto Jerusalem OutlineSerto KharputSerto MalankaraSerto MardinSerto UrhoySilomSkiaSymbolSystem FontTahomaTimes New RomanTrattatelloTrebuchet MSVerdanaWebdingsWingdingsWingdings 2Wingdings 3YaHei Consolas HybridZapf DingbatsZapfinocmb10cmex10cmmi10cmr10cmss10cmsy10cmtt10
Fontconfig warning: ignoring UTF-8: not a valid region tagChinese font list get from fc-list:.Hiragino Sans GB Interface.LastResort.PingFang HK.PingFang SC.PingFang TCArial Unicode MSFixedGB18030 BitmapHeiti SCHeiti TCHiragino Sans GBMicrosoft YaHei MonoPingFang HKPingFang SCPingFang TCSTSongSongti SCSongti TCYaHei Consolas Hybridthe fonts we can use:.LastResortMicrosoft YaHei MonoYaHei Consolas HybridArial Unicode MS
YaHei Consolas Hybrid 是我自己安装的字体,其他三个都是系统默认的,
这里选择了 YaHei Consolas Hybrid。
mpl.rcParams['font.sans-serif'] = ['YaHei Consolas Hybrid'] #指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
这篇关于Linux/MacOS下matplotlib能正常显示的中文字体选择的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!