Matplotlib 饼状图中的文字中文乱码问题

今天用matplotlib画饼状图时候遇到中文乱码,一般遇到中文乱码有两种通用的解决方法,一种是修改matplotlibrc,通过修改matplotlibrc中的font.sans-serif添加中文,一种是直接在代码中通过rcParams修改字体,既然遇到乱码当然先用传统方法试试,代码如下:

可以看到虽然title的中文问题解决了,但是饼状图的中文依然显示乱码,下面试试修改matplotlibrc文件

遇到中文可以看出乱码问题,然后尝试了修改matplotlibrc文件,但是问题却依然没得到解决:

Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.familyfont.sans-serif两行前的#,并在font.sans-serif后添加微软雅黑字体(Microsoft YaHei),但是发现原来还是不行,究竟是什么问题呢?

______________________________________________________

后来发现ax.pie()函数返回值里面有text实例如下图:

1ax.pie(np.array(game_app["download_times"])[:5],labels=game_app["name"][:5],autopct='%1.1f%%') 2× 3Out[73]: 4([<matplotlib.patches.Wedge at 0xd1b7450>, 5  <matplotlib.patches.Wedge at 0xd4686f0>, 6  <matplotlib.patches.Wedge at 0xd5757b0>, 7  <matplotlib.patches.Wedge at 0xd58b4f0>, 8  <matplotlib.patches.Wedge at 0xd58b950>], 9 [<matplotlib.text.Text at 0xd531710>, 10  <matplotlib.text.Text at 0xd575250>, 11  <matplotlib.text.Text at 0xd575b90>, 12  <matplotlib.text.Text at 0xd58b3d0>, 13  <matplotlib.text.Text at 0xd58bd30>], 14 [<matplotlib.text.Text at 0xd4689d0>, 15  <matplotlib.text.Text at 0xd575450>, 16  <matplotlib.text.Text at 0xd575f70>, 17  <matplotlib.text.Text at 0xd58b690>, 18  <matplotlib.text.Text at 0xd58bfb0>])

查看了一下text实例的fontname名称,竟然不是Microsoft YaHei还是Bitstream Vera Sans,原来问题在这,图形中的text实例并没有受到影响:

1= ax.pie(np.array(game_app["download_times"])[:5],labels=game_app["name"][:5],autopct='%1.1f%%') 2a[1][1].get_fontname() 3Out[46]: 4'Bitstream Vera Sans'

那么只能直接改text实例了,

1pie = ax.pie(np.array(game_app["download_times"])[:5],labels=game_app["name"][:5],autopct='%1.1f%%') 2#图形中的文字无法通过rcParams设置 3for font in pie[1]: 4    font.set_fontproperties(mpl.font_manager.FontProperties( 5            fname='L:/Python27/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/simfang.ttf'))

修改后饼状图图形中的中文乱码可以解决了:

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid

SpringBoot整合Redis乱码原因及解决方案

问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa

Matplotlib 饼状图中的文字中文乱码问题 - HelloWorld