matplotlib、seaborn 展示中文字体
我们经常使用matplotlib
、seaborn
绘制各种图标但是失望的是他们对中文字符不支持。
matplotlib 展示中文
查找系统上中文字体的安装位置,我选用的是宋体,然后拷贝到可读目录下~/fonts/Songti.ttc
mac: /System/Library/Fonts/Supplemental/Songti.ttc
window: C:WINDOWSFonts中文字体.ttf
linux: /usr/share/fonts
python 代码:
import matplotlib.font_manager as fm,os
import matplotlib.pyplot as plt
# 添加字体
myfont = fm.FontProperties(fname=os.path.join(os.getenv('HOME'),'fonts/Songti.ttc'),size=10)
plt.xlabel('长度',fontproperties=myfont)
plt.ylabel('宽度',fontproperties=myfont)
seaborn 展示中文
方法一:提供字体文件
-
查找系统上中文字体的安装位置,我选用的是宋体,然后拷贝到可读目录下
~/fonts/Songti.ttc
mac: /System/Library/Fonts/Supplemental/Songti.ttc window: C:WINDOWSFonts中文字体.ttf linux: /usr/share/fonts
-
Python 代码:
import os import matplotlib.font_manager as fm import seaborn as sns import pandas as pd # 添加字体 myfont = fm.FontProperties(fname=os.path.join(os.getenv('HOME'),'fonts/Songti.ttc'),size=10) sns.set(font=myfont.get_name()) tips = pd.DataFrame({ 'time':["午餐","晚餐"], 'total_bill':[1,2], }) sns.pointplot(x="time", y="total_bill", data=tips)
方法二:添加字体
-
查看
matplotlib
默认字体from matplotlib.font_manager import findfont, FontProperties findfont(FontProperties(family=FontProperties().get_family())) # out: /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf
-
查找
matplotlibrc
的路径import matplotlib matplotlib.matplotlib_fname() # out: /home/xxx/venv/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
-
修改
matplotlibrc
vim /home/xxx/venv/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
取消注释
font.family
和font.sans-serif
,然后在font.sans-serif
后追加你刚才的字符集名:# 获取字符集名 import matplotlib.font_manager as fm myfont = fm.FontProperties(fname=os.path.join(os.getenv('HOME'),'fonts/Songti.ttc'),size=10) myfont.get_name() # out: Songti SC
修改:
font.family: sans-serif #font.style: normal #font.variant: normal #font.weight: normal #font.stretch: normal #font.size: 10.0 #font.serif: DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L,Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif font.sans-serif: Songti SC, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif #font.cursive: Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive #font.fantasy: Comic Neue, Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy #font.monospace: DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
-
移除
matplotlib
缓存rm -rf ~/.matplotlib
-
拷贝你的字体文件到
matplotlib
字体目录下cp /System/Library/Fonts/Supplemental/Songti.ttc /Users/Andrew/miniconda3/envs/technical-note/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
-
重启
jupyter Notebook
内核,然后就可以了。
方法三:直接替换默认字体
-
查看
matplotlib
默认字体from matplotlib.font_manager import findfont, FontProperties findfont(FontProperties(family=FontProperties().get_family())) # out: /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf
-
备份字体
cp /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf.bp
-
替换
cp /System/Library/Fonts/Supplemental/Songti.ttc /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf
-
重启
jupyter Notebook
内核,然后就可以了