登录    关于
马育民老师的博客

马育民的博客

QQ:65242847

Pandas数据可视化中文乱码问题

介绍

Pandas数据可视化依赖 matplotlib,matplotlib包默认只支持 ASCII码不支持unicode码

解决方法:

  1. 修改 matplotlib 默认字体(推荐)
  2. 修改matplotlib的matplotlibrc配置文件、

查看系统支持哪些字体

from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print (mat_fonts)

linux系统查看中文字体:

output = subprocess.check_output(
    'fc-list :lang=zh -f "%{family}\n"', shell=True)
print ('*' * 10, '系统可用的中文字体', '*' * 10)
print (output)
zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
available = mat_fonts & zh_fonts
print ('*' * 10, '可用的字体', '*' * 10)
for f in available:
    print (f)

执行结果:

{'DejaVu Sans Mono', 'BrowalliaUPC', 'MoolBoran', 'STIXSizeTwoSym', 'Franklin Gothic Medium', 'Pristina', 'Gabriola', 'Times New Roman', 'STSong', 'Gadugi', 'Leelawadee', 'Malgun Gothic', 'MS Gothic', 'Juice ITC', 'Century', 'cmr10', 'Comic Sans MS', 'Batang', 'Segoe Script', 'Vrinda', 'FangSong', 'cmex10', 'FreesiaUPC', 'STIXSizeFiveSym', 'CordiaUPC', 'Urdu Typesetting', 'MT Extra', 'Lucida Handwriting', 'Arial', 'cmmi10', 'Segoe UI', 'Segoe Print', 'LilyUPC', 'Calibri', 'Lucida Sans Unicode', 'STIXGeneral', 'cmtt10', 'MS Mincho', 'Corbel', 'KodchiangUPC', 'STFangsong', 'DejaVu Sans', 'Book Antiqua', 'YouYuan', 'DejaVu Sans Display', 'Dubai', 'Segoe UI Symbol', 'Microsoft PhagsPa', 'Trebuchet MS', 'Shonar Bangla', 'Papyrus', 'Kartika', 'MV Boli', 'Georgia', 'Nirmala UI', 'DokChampa', 'Kalinga', 'Tahoma', 'Marlett', 'STXinwei', 'Consolas', 'Microsoft Yi Baiti', 'STCaiyun', 'Impact', 'Aharoni', 'STXihei', 'Ebrima', 'Sakkal Majalla', 'FrankRuehl', 'STIXNonUnicode', 'STIXSizeFourSym', 'FZShuTi', 'Microsoft Uighur', 'DilleniaUPC', 'Miriam Fixed', 'Narkisim', 'STXingkai', 'Wingdings', 'Kristen ITC', 'STIXSizeThreeSym', 'DejaVu Serif Display', 'Tempus Sans ITC', 'Vani', 'Freestyle Script', 'Andalus', 'Palatino Linotype', 'MS Reference Specialty', 'Century Gothic', 'Cordia New', 'DejaVu Serif', 'David', 'Microsoft JhengHei', 'Tunga', 'Estrangelo Edessa', 'Nyala', 'Bookshelf Symbol 7', 'Mangal', 'Aldhabi', 'Vijaya', 'Mongolian Baiti', 'Lao UI', 'Microsoft Tai Le', 'Kokila', 'French Script MT', 'STKaiti', 'Mistral', 'Gautami', 'Browallia New', 'cmb10', 'Javanese Text', 'Microsoft Sans Serif', 'Aparajita', 'Latha', 'SimSun-ExtB', 'Webdings', 'Shruti', 'Gulim', 'SimSun', 'Lucida Console', 'Courier New', 'Leelawadee UI', 'Microsoft Himalaya', 'FZYaoTi', 'DengXian', 'Wingdings 2', 'Raavi', 'Wingdings 3', 'Constantia', 'STHupo', 'Verdana', 'Khmer UI', 'EucrosiaUPC', 'Miriam', 'Angsana
New', 'STIXSizeOneSym', 'Arial Unicode MS', 'MingLiU', 'Traditional Arabic', 'Axure Handwriting', 'Candara', 'FZLanTingHeiS-UL-GB', 'Segoe UI Emoji', 'Symbol', 'Plantagenet Cherokee', 'STLiti', 'Microsoft New Tai Lue', 'cmsy10', 'KaiTi',
'Arabic Typesetting', 'cmss10', 'Yu Gothic', 'Sylfaen', 'Simplified Arabic Fixed', 'LiSu', 'STZhongsong', 'Myanmar Text', 'Levenim MT', 'Meiryo', 'Cambria', 'Microsoft YaHei', 'Euphemia', 'SimHei', 'Sitka Small', 'Simplified Arabic', 'MS
Reference Sans Serif', 'IrisUPC', 'Utsaah', 'Iskoola Pota', 'JasmineUPC', 'Bradley Hand ITC', 'DFKai-SB', 'Gisha', 'Yu Mincho', 'AngsanaUPC', 'Rod', 'DaunPenh', 'MingLiU-ExtB'}

在windows系统中,可以看到 Microsoft YaHei 微软雅黑

修改matplotlib字体

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] #可以正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #保存图片时能够正常显示负号

测试

import pandas as pd
import matplotlib.pyplot as plt

df=pd.DataFrame([['韩梅梅',18,1.65],['李雷',19,1.82],['lucy',18,1.70]],columns=['姓名','年龄','身高'])
print(df)
df[['姓名','身高']].plot()
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.show()

原文出处:https://malaoshi.top/show_1EF6VdY1BTnE.html