Seaborn: The solution of display chinese in seaborn plot

Created on 12 Sep 2016  ·  8Comments  ·  Source: mwaskom/seaborn

As #829 and #592 said, the reason is "seaborn override default fonts specified in matplotlibrc configs".
Thus, you can set the default seaborn font style after import the seaborn as follows:

import seaborn as sns
sns.set_style("darkgrid",{"font.sans-serif":['simhei', 'Arial']})

The simhei is the chinese font, and make sure the simhei.ttf already in your "Lib\site-packages\matplotlib\mpl-data\fonts\ttf" folder.

This method just change the seaborn font setting temporarily, however I do not know how to do that permanently.

Most helpful comment

Thanks @githubzhanglei , it works fine under python 3, here is my result. Just to be sure to set mpl first and then set seaborn.

import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
import seaborn as sns
sns.set_style("darkgrid",{"font.sans-serif":['simhei', 'Arial']})

qq

All 8 comments

Hi, I tried this code, my enviroment is Anaconda3 and Python3.5, but it seems not work, 中文依旧显示乱码啊啊啊啊啊啊, 咋搞啊 ,我日, 我吧代码加进去了, 我也确保了那个文件夹有黑体字体文件, 我的matplolib是正常的, 可是依旧显示乱码, 咋搞啊, 求大神指点一二

Hi Elgins, I don't know anything about the Python3, :-)

Thanks @githubzhanglei , it works fine under python 3, here is my result. Just to be sure to set mpl first and then set seaborn.

import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
import seaborn as sns
sns.set_style("darkgrid",{"font.sans-serif":['simhei', 'Arial']})

qq

@cqcn1991 When I try that I get this warning

/home/blomqvist/miniconda3/envs/notebook3/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

@viblo Seems like you are not under windows? Maybe you don't have 'SimHei' at all. Try changing it to a font that's available in your computer.

@cqcn1991 Right, Im on Ubuntu. I now got it to work, by using "Droid Sans Fallback" as my font. Thanks for the help!

I still think it would be good if seaborn could default to a good font by default so each user doesnt have to do this by themselves.

Hi, I tried this code, my enviroment is Anaconda3 and Python3.5, but it seems not work, 中文依旧显示乱码啊啊啊啊啊啊, 咋搞啊 ,我日, 我吧代码加进去了, 我也确保了那个文件夹有黑体字体文件, 我的matplolib是正常的, 可是依旧显示乱码, 咋搞啊, 求大神指点一二

请问这个问题解决了吗?我也不行。。。

If still seeing error after putting SimHei.ttf into site-packages/matplotlib/mpl-data/fonts/ttf, you might try to clear font cache. For centos, just go to ~/.cache/matplotlib and remove cached font list file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bondarevts picture bondarevts  ·  3Comments

TDaltonC picture TDaltonC  ·  3Comments

wenhaosun picture wenhaosun  ·  3Comments

sofiatti picture sofiatti  ·  4Comments

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  ·  4Comments