问题描述:
在使用seaborn画热力图的过程中,产生错误RuntimeWarning: Glyph xxxxx missing from current font.
系统:windows 10
python版本:python 3.6
错误原因:中文字体问题
解决办法:
1、matplotlib
这个问题在之前使用matplotlib时候遇到过 ,也按照网上的设置方法试过了,比如:
方法一:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
方法二:
通过配置文件matplotlibrc来设置字体
具体配置过程大家去看看其他博文,都挺详细的,之前对于matplotlib就没问题,但是用seaborn画图的话,我试过以上方法之后都还是会有警告,热力图中的中文还是不显示,中文处显示的是框框(怎么肥事!!!!)
2、seaborn
在看了一些博文之后,发现seaborn画图是要在画图之前加入设置字体的代码,而之前matplotlib设置的时候我是直接在import matplotlib.pyplot as plt之后加的,也就是在所有代码之前直接加的,如果用seaborn有字体问题,可能是设置字体的代码位置不对,可以试试以下方法看能不能解决:
在 画图前(画图前 画图前!!!) 加入下面代码(其实是和之前一样样的代码哈哈):
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
例如:
以下几行是我自己画热力图的代码
修改前:
import seaborn as sns
sns.set()
f, ax = plt.subplots()
sns.heatmap(confusion, annot=True, ax=ax) # 画热力图
ax.set_title('confusion matrix 图片') # 标题
ax.set_xlabel('Predict') # x轴
ax.set_ylabel('True') # y轴
plt.show()
修改后:
import seaborn as sns
sns.set()
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
f, ax = plt.subplots()
sns.heatmap(confusion, annot=True, ax=ax) # 画热力图
ax.set_title('confusion matrix 图片') # 标题
ax.set_xlabel('Predict') # x轴
ax.set_ylabel('True') # y轴
plt.show()
热力图中的中文就显示出来了哈哈哈哈哈哈!!!
来个小尾巴:第一次写博文,记录自己所遇到的问题,有不对之处那我就改正!!!