【python】数据可视化——解决matplotlib显示中文乱码

问题描述:标题出现乱码

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.figure(figsize=(10, 5))
plt.plot(x, y, 'C0', linewidth=3, linestyle='solid')
plt.title('第一副图')

plt.show()

在这里插入图片描述

报错信息

D:\anaconda3\envs\test1\lib\tkinter_init_.py:839: UserWarning: Glyph 31532 (\N{CJK UNIFIED IDEOGRAPH-7B2C}) missing from current font.
func(*args)
D:\anaconda3\envs\test1\lib\tkinter_init_.py:839: UserWarning: Glyph 19968 (\N{CJK UNIFIED IDEOGRAPH-4E00}) missing from current font.
func(*args)
D:\anaconda3\envs\test1\lib\tkinter_init_.py:839: UserWarning: Glyph 21103 (\N{CJK UNIFIED IDEOGRAPH-526F}) missing from current font.
func(*args)
D:\anaconda3\envs\test1\lib\tkinter_init_.py:839: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font.
func(*args)
这个警告信息是由于matplotlib默认使⽤的字体中不包含中⽂字符引起的

解决办法一

更换字体:首先,确保你安装了支持中文字符的字体,例如"SimHei"、"Microsoft YaHei"等。然后,通过设置Matplotlib的全局字体来更换字体。

import matplotlib

matplotlib.rcParams['font.sans-serif'] = ['SimHei'] #font.sans-serif参数来指定"SimHei"字体
matplotlib.rcParams['axes.unicode_minus'] = False	#axes.unicode_minus参数用于显示负号

演示

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

plt.figure(figsize=(10, 5))
plt.plot(x, y, 'C0', linewidth=3, linestyle='solid')
plt.title('第一副图')

plt.show()

解决办法二:永久解决办法

import matplotlib
print(matplotlib.matplotlib_fname())

D:\anaconda3\envs\test1\lib\site-packages\matplotlib\mpl-data\matplotlibrc
打开这个文件

找到#font.family:font.serif: 开头的这两行
将两⾏的注释#去掉,并在font.serif: 后添加⾃⼰想加入的中文字体名
在这里插入图片描述
去除行首#,将True改为False,如axes.unicode_minus: False

运行效果

在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
UserWarning: Glyph 29992 (\N{CJK UNIFIED IDEOGRAPH-7528}) missing from current font. 这个警告是由于当前字体缺少字形"29992"所引起的。字形缺失可能是由于字体文件损坏或不完整导致的。要解决这个问题,可以尝试以下方法: 1. 更新字体文件:检查系统中使用的字体文件是否是最新版本,如果不是,可以尝试更新字体文件。 2. 安装缺失字形的字体:如果缺失的字形属于某个特定字体,可以尝试安装该字体的完整版本。 3. 更改字体设置:在使用的软件中,可以尝试更改字体设置,选择一个包含所需字形的字体。 4. 使用备用字形:某些软件或编辑器可能提供了使用备用字形的选项。可以尝试启用此选项来避免缺失字形的问题。 需要注意的是,由于汉字的复杂性,特定字体可能会缺少某些字形。因此,在处理汉字时,确保使用完整的字体文件是非常重要的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [UserWarning: Glyph 30005 (\N{CJK UNIFIED IDEOGRAPH-7535}) missing from current font解决方式方法](https://blog.csdn.net/qq_43874317/article/details/128160515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [《Python编程:从入门到实践》读书笔记:第15章 生成数据](https://blog.csdn.net/weixin_44850744/article/details/124312516)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz的学习笔记本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值