问题描述
python中数据可视化工具matplotlib的使用时,使用中文标题、中文坐标轴时出现乱码。
解决方法
STEP1:CSDN、官网等下载中文字体库SimHei.ttf文件
STEP2:拷贝字体到 /usr/share/fonts(基本每个Linux系统都有这个目录,需要root权限)
STEP3:删除缓存文件,rm -rf ~/.cache/matplotlib/*
STEP4:尝试以下两种方法:,方法一不行尝试方法二
方法一:运行下面两段代码,后续无需运行。
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
方法二:在有中文输出的地方,增加一个属性:fontproperties
from matplotlib.font_manager import *
myfont = FontProperties(fname='/usr/share/fonts/SimHei.ttf')
方法二举例:
sns.countplot(sr_text_202303.first_assign_dept,data = sr_text_202303)
plt.xticks(rotation=90, fontsize=14,FontProperties=myfont)
plt.title("电影票房收入对比",FontProperties=myfont)# 使用font=my_font指定字体
plt.show()