程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Python中使用matplotlib时显示中文乱码_(或更改字体)

发布于2023-10-15 20:55     阅读(10126)     评论(0)     点赞(0)     收藏(4)


一、问题描述

        在使用matplotlib绘制可视化图表时,图表的中文显示乱码,只能正常显示英文内容,如下图所示:

二、问题分析

     一般显示乱码是由于编码问题导致的,而matplotlib 默认使用ASCII 编码,但是当使用pyplot时,是支持unicode编码的,只是默认字体是英文字体,导致中文无法正常显示,所以显示中文乱码。

三、解决方法

说明:我当前使用的环境是【Python 3.11.1】、【matplotlib  3.7.1】版本

  1. #查看python版本命令
  2. python --version
  3. #安装matplotlib命令:
  4. pip install matplotlib
  5. #查看当前安装的所有包和对应版本命令
  6. pip list

 

  

3.1、方法一【设置全局的字体】

《1》导入matplotlib和字体依赖模块

  1. from matplotlib import pyplot as plt
  2. from matplotlib import font_manager

《2》查看当前系统已经安装的字体,方便在全局设置中使用字体名称

  1. fontnamelist = font_manager.get_font_names()
  2. print(fontnamelist)

这是获取到的Windows10系统自带所有字体的名称 

  1. ['Harrington', 'STXinwei', 'Microsoft YaHei', 'Bookshelf Symbol 7', 'Edwardian Script ITC', 'Haettenschweiler', 'Franklin Gothic Demi', 'Niagara Engraved', 'cmr10', 'DejaVu Serif', 'Britannic Bold', 'Gigi', 'Microsoft JhengHei', 'French Script MT', 'Microsoft Uighur', 'YouYuan', 'Seagull: Paint v1.0', 'Baskerville Old Face', 'Times', 'Cooper Black', 'Tw Cen MT Condensed', 'Engravers MT', 'ITC Bookman', 'Brush Script MT', 'Calisto MT', 'Cambria', 'STSong', 'STIXSizeFourSym', 'SimSun', 'STXingkai', 'Informal Roman', 'Courier', 'Onyx', 'Segoe MDL2 Assets', 'SimSun-ExtB', 'OCR-A-Seagull', 'Verdana', 'Mongolian Baiti', 'Wingdings', 'FangSong', 'Playbill', 'Poor Richard', 'STHupo', 'Comic Sans MS', 'Curlz MT', 'STIXSizeThreeSym', 'Maiandra GD', 'Pristina', 'Agency FB', 'Gill Sans MT', 'Microsoft New Tai Lue', 'Tahoma', 'Bahnschrift', 'Script MT Bold', 'Albertus Extra Bold', 'CourierPS', 'Stencil', 'Microsoft Tai Le', 'Helvetica', 'Impact', 'Garamond', 'FZYaoTi', 'Franklin Gothic Heavy', 'Bell MT', 'Segoe Print', 'Book Antiqua', 'Lucida Fax', 'MS Outlook', 'Albertus Medium', 'Perpetua', 'Candara', 'Bodoni MT', 'CG Times', 'MS Reference Specialty', 'Papyrus', 'Juice ITC', 'ITC Avant Garde Gothic', 'cmsy10', 'Wingdings 2', 'FZShuTi', 'MS Gothic', 'Lucida Sans', 'Univers Condensed', 'HoloLens MDL2 Assets', 'cmmi10', 'BarCode', 'Rockwell Extra Bold', 'DengXian', 'Blackadder ITC', 'Rockwell', 'Matura MT Script Capitals', 'Perpetua Titling MT', 'Copperplate Gothic Light', 'Berlin Sans FB Demi', 'Old English Text MT', 'Wingdings 3', 'ITC Zapf Chancery', 'Consolas', 'Coronet', 'Eras Light ITC', 'STFangsong', 'Gadugi', 'cmss10', 'Lucida Bright', 'Century Gothic', 'Sitka Small', 'Seagull: Risk Phrases v1.0', 'Monotype Corsiva', 'OCR A Extended', 'Eras Bold ITC', 'STCaiyun', 'MT
  2. Extra', 'Letter Gothic', 'Footlight MT Light', 'STKaiti', 'Webdings', 'SymbolPS', 'Segoe UI Symbol', 'Yu Gothic', 'Malgun Gothic', 'Microsoft Sans Serif', 'Gill Sans MT Condensed', 'Bernard MT Condensed', 'Palatino Linotype', 'STIXNonUnicode', 'Snap ITC', 'DejaVu Sans Display', 'STIXGeneral', 'Kristen ITC', 'Helvetica Narrow', 'Franklin Gothic Medium', 'STIXSizeOneSym', 'Seagull: ADR v1.0', 'cmb10', 'Nirmala UI', 'Niagara Solid', 'Lucida Sans Unicode', 'Lucida Sans Typewriter', 'Vivaldi', 'STIXSizeTwoSym', 'Trebuchet MS', 'Broadway', 'KaiTi', 'Wide Latin', 'Tw Cen MT Condensed Extra Bold', 'Bradley Hand ITC', 'Centaur', 'cmex10', 'Eras Demi ITC', 'Kunstler Script', 'Century', 'Algerian', 'Vladimir Script', 'Franklin Gothic Book', 'Magneto', 'Modern No. 20', 'Felix Titling', 'Javanese Text', 'MV Boli', 'Segoe UI', 'Constantia', 'Segoe Script', 'Franklin Gothic Medium Cond', 'MingLiU-ExtB', 'Seagull: Electrical v1.0', 'STXihei', 'Courier New', 'Century Schoolbook', 'Microsoft PhagsPa', 'Viner Hand ITC', 'Forte', 'High Tower Text', 'Marigold', 'STLiti', 'Goudy Old Style', 'Harlow Solid Italic', 'Palace Script MT', 'Tw Cen MT', 'Ebrima', 'Eras Medium ITC', 'Seagull: Logos v1.0', 'Colonna MT', 'Gill Sans Ultra Bold Condensed', 'Franklin Gothic Demi Cond', 'Copperplate Gothic Bold', 'Georgia', 'SimHei', 'Seagull: Hazard v1.0', 'Gloucester MT Extra Condensed', 'OCR-B-Seagull', 'STZhongsong', 'Seagull: Emergency v1.0', 'Arial', 'Calibri', 'Ravie', 'Microsoft Himalaya', 'Seagull: Recycling v1.0', 'Seagull: Warning v1.0', 'Seagull: Textile Care v1.0',
  3. 'Clarendon Condensed', 'DejaVu Sans', 'Bookman Old Style', 'New Century Schoolbook', 'Imprint MT Shadow', 'ITC Zapf Dingbats', 'STIXSizeFiveSym', 'Freestyle Script', 'Gill Sans Ultra Bold', 'Bauhaus 93', 'Leelawadee', 'Sylfaen', 'Dubai', 'Showcard Gothic', 'Rockwell Condensed', 'Univers', 'Rage Italic', 'Arial Rounded MT Bold', 'Gabriola', 'Mistral', 'Segoe UI Emoji', 'Leelawadee UI', 'Ink Free', 'Antique Olive', 'Palatino', 'DejaVu Serif Display', 'Berlin Sans FB', 'Lucida Console', 'Elephant', 'CG Omega', 'Tempus Sans ITC', 'Corbel', 'MS Reference Sans Serif', 'Jokerman', 'Parchment', 'Gill Sans MT Ext Condensed Bold', 'Castellar', 'LiSu', 'Lucida Handwriting',
  4. 'Segoe UI Historic', 'Seagull: Packaging v1.0', 'Goudy Stout', 'Times New Roman', 'Symbol', 'DejaVu Sans Mono', 'Californian FB', 'Myanmar Text', 'cmtt10', 'Lucida Calligraphy', 'Chiller', 'Microsoft Yi Baiti']

 《3》设置全局字体

设置全局字体使用的是当前系统已经安装的字体,比如我这里使用的是楷体

plt.rcParams['font.sans-serif']=['kaiti']

《4》解决图表中"-"负号显示乱码问题

plt.rcParams["axes.unicode_minus"]=False #解决图像中的"-"负号的乱码问题

《5》 展示效果

3.2、方法二【设置局部字体】

《1》导入matplotlib和字体依赖模块

  1. from matplotlib import pyplot as plt
  2. from matplotlib import font_manager

《2》查看当前系统已经安装的字体,方便在全局设置中使用字体名称

  1. fontnamelist = font_manager.get_font_names()
  2. print(fontnamelist)

《3》设置局部字体

 比如我这里是获取了【仿宋】、【楷体】的字体;然后在单独给指定的模块设置对应字体(即:图表标题是仿宋字体,而X和Y轴上显示的则使用楷体字体)。

  1. fangsong_font = font_manager.FontProperties(family="FangSong", size=20)
  2. kaiti_font = font_manager.FontProperties(family="KaiTi", size=20)
  3. plt.title("测试物体的平均值数据:",fontsize=18,fontproperties=fangsong_font)
  4. plt.xlabel('当天时间',fontsize=16,fontproperties=kaiti_font)
  5. plt.ylabel('平均数值',fontsize=16,fontproperties=kaiti_font)

 

3.3、方法三【设置指定路径的局部字体】

《1》导入matplotlib和字体依赖模块

  1. from matplotlib import pyplot as plt
  2. from matplotlib import font_manager

《2》设置指定路径下的第三方字体

注意:fname后面的内容是指定路径下的字体

  1. siyuan_font = font_manager.FontProperties(fname = "fonts/SourceHanSansHWSC-Regular.otf",size=20)
  2. simyou_font = font_manager.FontProperties(fname = "fonts/SIMYOU.TTF",size=20)
  3. plt.title("测试物体的平均值数据:",fontsize=18,fontproperties=siyuan_font)
  4. plt.xlabel('当天时间',fontsize=16,fontproperties=simyou_font)
  5. plt.ylabel('平均数值',fontsize=16,fontproperties=simyou_font)

3.4、方法四【设置指定路径的全局字体】

《1》导入matplotlib和字体依赖模块

  1. from matplotlib import pyplot as plt
  2. from matplotlib import font_manager

《2》添加指定路径下的第三方字体

  1. font_dirs = ["fonts"] #fonts是当前程序下存放第三方字体的文件夹名称
  2. font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
  3. for fontfile in font_files:
  4. font_manager.fontManager.addfont(fontfile)

 《3》查看当前拥有的所有字体名称

  1. fontnamelist = font_manager.get_font_names()
  2. print(fontnamelist)

《4》设置第三方的字体为全局字体

plt.rcParams['font.sans-serif'] = ['Source Han Sans HW SC'] #设置全局字体(比如我这里添加的是思源宋体)

《5》展示效果

3.5、参考资料

Examples — Matplotlib 3.8.0.dev838+gbff46815c9 documentationhttps://matplotlib.org/devdocs/gallery/index.html

matplotlib.font_manager — Matplotlib 3.8.0.dev838+gbff46815c9 documentationhttps://matplotlib.org/devdocs/api/font_manager_api.html#matplotlib.font_manager.FontManagerMatplotlib中文乱码解决方案(两种方式)Matplotlib 默认字体并不支持中文,http://c.biancheng.net/matplotlib/9284.html

原文链接:https://blog.csdn.net/xiaochenXIHUA/article/details/130116221



所属网站分类: 技术文章 > 博客

作者:坚持就是胜利

链接:https://www.pythonheidong.com/blog/article/2026453/6f209f9b54dd8b7f275d/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)