最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, scipy, matplotlib, jupyter)等进行一些初步的数据挖掘和分析。
在使用matplotlib画图时,横坐标为中文,但是画出的条形图横坐标总是显示“框框”,就去查资料解决。感觉这应该是个比较常见的问题,网上的中文资料也确实很多,但是没有任何一个彻底解决了我遇到的问题。零零碎碎用了快3个小时的时间,才终于搞定。特此分享,希望能帮到有同样问题的童鞋。
运行环境:
python2.7
Linux Centos7
用conda安装的matplotlib和pandas
问题:
matplotlib画图,无法显示中文
问题原因:
linux操作系统以及matplotlib的字体库中,没有可用的中文字体
matplotlib包默认只支持ASCII码,不支持unicode码
网上资料总结:
修改matplotlib的资源配置文件,例如增加"Simhei"字体(这个字体并不是所有的linux系统都有的好嘛!修改了并没有作用)
给linux安装中文字体,并修改matplotlib的资源配置文件。(呵呵,并没有作用)
解决方案:
其实也是综合了网上的各种解决方案。总体说来有以下几个步骤:
1. 获取matplotlibrc文件所在路径。在jupyter notebook中获取:
1 import matplotlib2 matplotlib.matplotlib_fname()
例如,我的这个文件在:
u'~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
后续步骤会修改此文件中的font参数。
2. 看看系统中的所有字体,以及可用的中文字体。也是在jupyter nb中:
from matplotlib.font_manager import FontManagerimport subprocess fm = FontManager() mat_fonts = set(f.name for f in fm.ttflist)print mat_fonts output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)print '*' * 10, '系统可用的中文字体', '*' * 10print output zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n')) available = mat_fonts & zh_fontsprint '*' * 10, '可用的字体', '*' * 10for f in available:print f
做完上述操作,会发现“可用的字体”这里为空。因为没有中文字体给matplotlib用(所以才会中文都显示“框框”)
3. 假设操作系统中没有中文字体。此时下载一个ttf中文字体,并在cenos中安装。要安装那种系统能检测font-family的,否则无效。我在这个网站下载的:
解压rar文件。在 /usr/share/fonts 路径下创建存放此字体的文件夹yourfontdir,并下载的ttf文件复制到yourfontdir中(可以给文件改个英文名,方便操作)
4. 给cenos安装这个字体。
cd /usr/share/fonts/yourfontsdir#生成字体索引信息. 会显示字体的font-familysudo mkfontscale sudo mkfontdir#更新字体缓存:fc-cache
5. 修改matplotlibrc文件
修改步骤1中获取的matplotlibrc文件配置。
将font.family 部分注释去掉,并且在font.serif 支持字体加上一个中文字体。这里就加上刚才下载的中文字体的font-family. 可以通过 fc-list 命令查找一下(所以前面最好记下来)。我这里增加的是"WenQuanYi Zen Hei Mono"字体。
下面这句注释要去掉,不然中文减号也显示方块:
axes.unicode_minus : False
6. 这一步骤最重要!为matplotlib增加中文字体
完成步骤5后,再操作步骤2,会发现“可用的中文字体”已经有了刚才安装的字体,但是画图仍然不能显示中文。这是因为你这个字体给centos安装了、也告诉matplotlib要用这个字体了,但是,matplotlib找不到这个字体的ttf文件啊。。。。所以需要给它弄一个。
将下载的ttf字体复制一份到以下路径:
~/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf
并删除相关cache。在以下路径:
~/.cache/matplotlib
删除其中与字体有关的cache
7. 现在重新画个图试试。搞定。
参考资料:
以上是解决Linux系统中python matplotlib画图的中文显示问题的详细内容。更多信息请关注PHP中文网其他相关文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1
功能强大的PHP集成开发环境

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器