Matplotlib中文乱码的3种解决方案

Noah
Noah
发布时间:2026-10-13| 6人浏览过
原创

本文详细介绍解决Python Matplotlib库中文显示乱码的三种方法。包括修改全局配置文件matplotlibrc、通过代码重载配置以及自定义指定字体文件。重点讲解如何设置sans-serif字体及处理负号显示问题,帮助开发者快速实现高质量的中文图表输出。

前言

Matplotlib是Python中广泛使用的2D绘图库,能够在多种平台上生成具备出版级质量的图形,支持硬拷贝格式及交互式环境。该库适用于Python脚本、IPython Shell、Jupyter Notebook、Web应用服务器以及四种图形用户界面工具包。

然而,近期在使用Matplotlib时频繁遇到默认情况下的中文乱码问题。从原理上讲,Matplotlib本身支持中文显示,乱码产生的原因在于其默认配置文件中未包含中文字体的相关信息。

针对上述问题,以下是三种有效的解决路径:

方案一:修改全局配置文件

Matplotlib的配置信息存储于文件中,字体相关参数亦包含其中。要查询当前文件的具体存放目录,可调用get_configdir()函数进行获取。

import matplotlib
matplotlib.get_configdir()

该文件通常位于lib\site-packages\matplotlib\mpl-data\路径下。

其中涉及字体配置的关键部分如下所示:

#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默认使用的为sans-serif(无衬线字体)。观察字段可知,其预设值均为西文字体。此处的配置逻辑与CSS样式表中的字体设置类似,只需将系统中已存在的字体名称添加至该列表中即可(需注意,Matplotlib仅支持格式的字体文件)。

在编辑时,务必去除配置项前的注释符号#,使设置生效。

方案二:通过代码重载配置文件

import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题,或者转换负号为字符串

方案三:自定义指定字体

import numpy as np
import pylab as pl
import matplotlib.font_manager as fm

myfont = 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()

总结

以上内容即为解决Matplotlib中文乱码的全部方法,希望能为各位的学习或工作提供有价值的参考。若有任何疑问,欢迎留言交流,感谢对本站的支持。

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。