Matplotlib 显示中文为方框(乱码)的问题

Matplotlib 显示中文为方框(乱码)的问题(如下图所示)是由于缺少中文字体支持。解决方案:
在这里插入图片描述

方法 1:指定中文字体

直接设置 Matplotlib 使用支持中文的字体(如 SimHei、Microsoft YaHei 等):

import matplotlib.pyplot as plt

# 设置中文字体(以 Windows 的 "SimHei" 为例)
plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False    # 解决负号显示问题

# 示例绘图
x = [1, 2, 3]
y = [2, 4, 1]
plt.plot(x, y, label='中文图例')
plt.legend()
plt.show()

在这里插入图片描述

在 Windows 系统下,预装的常见中文字体及其对应的 matplotlib 字体名称如下:
  1. 微软系列字体
字体名称 (显示名)Matplotlib 字体名称 (fontfamily)备注
微软雅黑 (Microsoft YaHei)‘Microsoft YaHei’无衬线,适合屏幕显示
微软正黑 (Microsoft JhengHei)‘Microsoft JhengHei’繁体中文默认字体
新细明体 (PMingLiU)‘PMingLiU’旧版繁体明体
细明体 (MingLiU)‘MingLiU’旧版繁体明体(像素化明显)
  1. 黑体/等线系列
字体名称 (显示名)Matplotlib 字体名称 (fontfamily)备注
黑体 (SimHei)‘SimHei’简体中文默认黑体
等线 (DengXian)‘DengXian’Win8+ 默认无衬线字体
方正等线 (FZDaHei-B02)‘FZDaHei-B02’需手动安装
  1. 宋体/仿宋系列
字体名称 (显示名)Matplotlib 字体名称 (fontfamily)备注
宋体 (SimSun)‘SimSun’简体中文旧版默认字体
新宋体 (NSimSun)‘NSimSun’宋体的改进版
仿宋 (FangSong)‘FangSong’仿古印刷风格
  1. 楷体/隶书系列
字体名称 (显示名)Matplotlib 字体名称 (fontfamily)备注
楷体 (KaiTi)‘KaiTi’手写风格
隶书 (LiSu)‘LiSu’传统书法风格
华文楷体 (STKaiti)‘STKaiti’macOS/Windows 通用
  1. 其他中文字体
字体名称 (显示名)Matplotlib 字体名称 (fontfamily)备注
华文细黑 (STXihei)‘STXihei’macOS/Windows 通用
华文宋体 (STSong)‘STSong’macOS/Windows 通用
幼圆 (YouYuan)‘YouYuan’圆润风格

如何在 Matplotlib 中验证可用字体?运行以下代码查看所有已加载的字体:

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

# 列出所有支持中文的字体
for font in fm.fontManager.ttflist:
    if 'hei' in font.name.lower() or 'song' in font.name.lower() or 'kai' in font.name.lower():
        print(font.name, font.fname)

# 设置字体并测试
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 示例:使用微软雅黑
plt.plot([1, 2, 3], [2, 4, 1], label='测试中文')
plt.legend()
plt.show()
KaiTi C:\Windows\Fonts\simkai.ttf
Microsoft YaHei C:\Windows\Fonts\msyhbd.ttc
STKaiti C:\Windows\Fonts\STKAITI.TTF
KaiTi C:\windows\Fonts\simkai.ttf
STXingkai C:\windows\Fonts\STXINGKA.TTF
SimHei C:\windows\Fonts\simhei.ttf
FangSong C:\Windows\Fonts\simfang.ttf
Microsoft YaHei C:\windows\Fonts\msyhbd.ttc
Microsoft JhengHei C:\windows\Fonts\msjh.ttc
STFangsong C:\windows\Fonts\STFANGSO.TTF
Microsoft JhengHei C:\Windows\Fonts\msjhbd.ttc
Microsoft JhengHei C:\windows\Fonts\msjhbd.ttc
STXihei C:\windows\Fonts\STXIHEI.TTF
STXingkai C:\Windows\Fonts\STXINGKA.TTF
FangSong C:\windows\Fonts\simfang.ttf
STXihei C:\Windows\Fonts\STXIHEI.TTF
Microsoft YaHei C:\Windows\Fonts\msyhl.ttc
STFangsong C:\Windows\Fonts\STFANGSO.TTF
Microsoft YaHei C:\windows\Fonts\msyhl.ttc
STZhongsong C:\Windows\Fonts\STZHONGS.TTF
STSong C:\windows\Fonts\STSONG.TTF
Microsoft JhengHei C:\Windows\Fonts\msjhl.ttc
Microsoft YaHei C:\Windows\Fonts\msyh.ttc
STZhongsong C:\windows\Fonts\STZHONGS.TTF
Microsoft JhengHei C:\Windows\Fonts\msjh.ttc
STKaiti C:\windows\Fonts\STKAITI.TTF
STSong C:\Windows\Fonts\STSONG.TTF
SimHei C:\Windows\Fonts\simhei.ttf
Microsoft JhengHei C:\windows\Fonts\msjhl.ttc
Microsoft YaHei C:\windows\Fonts\msyh.ttc
### 解决 Matplotlib 中文显示方框问题 在 macOS 上运行 Matplotlib 时,如果发现中文字符显示方框,通常是因为默认字体不支持中文字符。以下是具体的解决方案: #### 方法一:设置合适的中文字体 可以通过修改 `plt.rcParams` 来指定支持中文的字体。例如,在 Windows 系统中常用的 SimHei 字体可能无法直接用于 macOS。可以尝试以下代码来加载适合 macOS 的字体[^2]。 ```python import matplotlib.pyplot as plt # 设置中文字体(macOS 常用的字体) plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # 正确显示负号 plt.rcParams['axes.unicode_minus'] = False ``` #### 方法二:查找并配置本地可用字体 为了确保使用的字体确实支持中文字符,可以先查看当前系统中的可用字体列表。通过以下代码获取所有可用字体名称[^1]: ```python from matplotlib.font_manager import fontManager for font in fontManager.ttflist: print(font.name) ``` 从打印的结果中挑选一个支持中文的字体名(如 Arial Unicode MS 或其他),将其替换到 `plt.rcParams['font.sans-serif']` 配置项中。 #### 方法三:更新 `matplotlibrc` 文件 另一种方法是编辑 Matplotlib 的全局配置文件 `matplotlibrc`。根据引用信息,该文件位于 `/CtripSpider-master/venv2/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc` 路径下[^1]。打开此文件后,找到如下两行内容并进行相应调整: ``` font.sans-serif : Arial Unicode MS axes.unicode_minus : False ``` 保存更改后重新启动 Python 环境即可生效。 --- ### 注意事项 - 如果仍然遇到问题,请确认所选字体是否已安装于操作系统中。 - 对于虚拟环境下的项目,建议优先采用动态设置方式(即通过脚本而非修改配置文件实现)以便保持项目的可移植性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值