python 折线图中文乱码_python:使用matplotlib画图时,中文乱码的问题

在Python中使用matplotlib绘制折线图时遇到中文乱码问题,可以按照以下步骤解决:1) 下载simhei.ttf字体;2) 找到matplotlib字体路径和缓存路径;3) 将字体文件放入指定目录;4) 修改matplotlibrc配置文件,添加SimHei字体并设置为默认;5) 删除字体缓存;6) 重启IDE。示例代码展示了配置后如何绘制包含中文标签的折线图。
摘要由CSDN通过智能技术生成

中文乱码:

错误信息:RuntimeWarning: Glyph 23398 missing from current font.

解决方案:

1、下载字体simhei.ttf

2、查找字体路径和字体缓存路径:

importmatplotlib#查找字体路径

print(matplotlib.matplotlib_fname())#查找字体缓存路径

print(matplotlib.get_cachedir())

结果:

/Users/apple/opt/anaconda2/envs/python37/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

/Users/apple/.matplotlib

3、将下载好的字体文件,存放到目录:(根据上面的结果,找到字体存放位置)

/Users/apple/opt/anaconda2/envs/python37/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

4、修改/Users/apple/opt/anaconda2/envs/python37/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc文件:

font.family : sans-serif#去掉前面的#

font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif#去掉前面的#,并在冒号后面添加SimHei

axes.unicode_minus : False#去掉前面的#,并将True改为False

5、删除缓存:

rm -rf /Users/apple/.matplotlib

6.重启 IDE

案例源码:

#coding:utf-8

importpandas as pdimportmatplotlibimportmatplotlib.pyplot as plt## 查找字体路径#print(matplotlib.matplotlib_fname())## 查找字体缓存路径#print(matplotlib.get_cachedir())

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['font.serif'] = ['SimHei']

filename= './files/datatemplate4.xlsx'students=pd.read_excel(filename)

students.sort_values(by='Number', inplace=True, ascending=False)print(students)#students.plot.bar(x='Students', y='Number', color='blue', title='成绩统计')

plt.bar(students.Students, students.Number, color='blue')

plt.xticks(students.Students, rotation='90')

plt.xlabel('Students')

plt.ylabel('Number')

plt.title('成绩统计')

plt.tight_layout()

plt.show()

数据文件格式:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值