解决pandas 作图无法显示中文的问题
2018-05-24 10:31
691 查看
最近开始使用 pandas 处理可视化数据,挖掘信息。但是在作图时遇到,无法显示中文的问题。
下面这段代码是统计 fujian1.csv 文件中 City 所在列中各个城市出现次数的代码。可是作图直方图时在 x 轴上无法显示中文。
import pandas as pd # Reading data locally df = pd.read_csv('fujian1.csv', encoding='gbk') counts = df['City'].value_counts() counts[counts > 1000].plot(kind = 'bar')
查了一些资料,找到的原因是 matplotlib 包默认只支持 ASCII 码,不支持 unicode 码。
解决方法,就是需要将 matplotlib 的安装目录下的 matplotlibrc 配置文件修改一下,将font.family 部分(大概在139行左右)注释去掉,并且在 font.serif 和 font.sans-serif 支持字体加上一个中文字体,如 SimHei:
font.family : sans-serif #font.style : normal #font.variant : normal #font.weight : medium #font.stretch : normal # note that font.size controls default text sizes. To configure # special text sizes tick labels, axes, labels, title, etc, see the rc # settings for axes and ticks. Special text sizes can be defined # relative to font.size, using the following values: xx-small, x-small, # small, medium, large, x-large, xx-large, larger, or smaller #font.size : 12.0 font.serif : SimHei, Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif #font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, cursive #font.fantasy : Comic Sans MS, Chicago, Charcoal, Impact, Western, fantasy #font.monospace : Bitstream Vera Sans Mono, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
最终实现了正常显示中文。
相关文章推荐
- 《ArcGIS Runtime SDK for Android开发笔记》——问题集:如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题(转载)
- wordpress mu无法显示中文问题的解决方法
- 问题2:解决直接输出数组的内容无法显示中文的问题
- unity在webgl端 输入框无法输入中文和中文显示问题的解决
- 解决mysql无法插入中文数据及插入后显示乱码的问题【亲测图文解说!】【转载】
- 理解SQLSERVER中的排序规则,解决无法正确显示中文字符的问题
- 解决SecureCRT中文显示乱码和全屏无法显示菜单的问题
- 解决python3爬虫无法显示中文的问题
- 解决pl/sql developer 中文字段显示乱码或无法用中文作查询条件的问题
- FlexPaper二次开发之解决中文路径无法显示问题
- 解决pandas无法在pycharm中使用plot()方法显示图像的问题
- 在使用Gson的toJson()方法转为json字符串的时候无法显示中文和符号的问题(已解决)
- Jira饼图上中文乱码,显示为“口”的解决办法,以及问题附件中文名时无法打开终极办法
- Fiddler2 中文手册“无法显示网页”问题的解决办法
- 理解SQLSERVER中的排序规则,解决无法正确显示中文字符的问题
- 解决linux/Ubuntu下Qt creater 界面程序在编译运行后无法显示中文或中文乱码问题(转)
- 解决JFreeChart无法正常显示中文问题
- 解决matplotlib绘制图片时中文无法正常显示的问题
- Debian下安装中文包和输入法【解决无法显示中文问题】
- 解决Ubuntu的PDF文档中文无法显示的问题