tp钱包安卓版下载 Python使用matplotlib时显示中文乱码解决方法(或更改字体)
发布日期:2025-05-05 10:11    点击次数:121

一、问题描述

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

https://www.shxhpack.com.cn

二、问题分析

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

三、解决方法

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

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

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

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

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

['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', tp官方下载安卓最新版2025 'FZShuTi',tp钱包官方安装下载 '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 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', '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', '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》设置全局字体

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

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

《5》 展示效果

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

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

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

《3》设置局部字体

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

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

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

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

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

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

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

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

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

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

《5》展示效果

3.5、参考资料 Examples — Matplotlib 3.8.0.dev838+gbff46815c9 documentation:https://matplotlib.org/devdocs/gallery/index.htmlmatplotlib.font_manager — Matplotlib 3.8.0.dev838+gbff46815c9 documentation:https://matplotlib.org/devdocs/api/font_manager_api.htmlMatplotlib中文乱码解决方案(两种方式)Matplotlib 默认字体并不支持中文,

总结 

到此这篇关于Python使用matplotlib时显示中文乱码解决方法(或更改字体)的文章就介绍到这了,更多相关matplotlib显示中文乱码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!



热点资讯
推荐资讯


Powered by tpwallet官网下载 @2013-2022 RSS地图 HTML地图

Copyright Powered by365建站 © 2013-2024