当前位置: 移动技术网 > IT编程>脚本编程>Python > 解决pandas 作图无法显示中文的问题

解决pandas 作图无法显示中文的问题

2019年06月17日  | 移动技术网IT编程  | 我要评论

条形码打印机,鄂q69888,coshion

最近开始使用 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

最终实现了正常显示中文。

以上这篇解决pandas 作图无法显示中文的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网