Matplotlib中文乱码的3种解决方案
技术  /  管理员 发布于 7年前   219
前言
Matplotlib是一个Python 2D绘图库,它可以在各种平台上以各种硬拷贝格式和交互式环境生成出具有出版品质的图形。 Matplotlib可用于Python脚本,Python和IPython shell,Jupyter笔记本,Web应用程序服务器和四个图形用户界面工具包。
然而最近在使用matplotlib默认情况会出现乱码问题,原则上matplotlib是支持中文的,只是在配置信息里没有中文字体的相关信息。
解决方法如下:
解决方案一:修改配置文件
matplotlib 从配置文件 matplotlibrc 中读取配置,字体相关内容也在其中。查询当前matplotlibrc 所在目录,可以用 get_configdir()函数:
import matplotlibmatplotlib.get_configdir()
通常存放位置:lib\site-packages\matplotlib\mpl-data\matplotlibrc
涉及到字体部分的设置内容为:
#font.family : sans-serif#font.style : normal#font.variant : normal#font.weight : normal#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 : 10.0#font.serif : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif#font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif#font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive#font.fantasy : Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy#font.monospace : DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
matplotlib 默认使用的 font.family 是 sans-serif,即无衬线字体,可以看到在font.sans-serif中设置的全部为西文字体,这里的设置和css样式文件中设置差不多,只需要添加系统存在的字体名称即可(需要注意的是,matplotlib:
只支持ttf格式的字体),设置时需要将注释符号#去除。
解决方案二:重载配置文件
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['font.serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题,或者转换负号为字符串
解决方案三:自定义字体
import numpy as npimport pylab as plimport matplotlib.font_manager as fmmyfont = fm.FontProperties(fname=r'D:\Fonts\simkai.ttf') # 设置字体t = np.arange(0.0,2.0 * np.pi,0.01) # 自变量取值范围s = np.sin(t) # 计算正弦函数值z = np.cos(t) # 计算余弦函数值pl.plot(t,s,label='正弦')pl.plot(t,z,label='余弦')pl.xlabel('x-变量',fontproperties=myfont,fontsize=24) #设置标签pl.ylabel('y-正弦余弦函数值',fontproperties=myfont,fontsize=24)pl.title('sin-cos函数图像',fontproperties=myfont,fontsize=32) #图像标题pl.legend(prop=myfont)pl.show()
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对AIDI的支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号