Python Matplotlib 中文字符显示问题解析:解决 Glyph 缺失警告(UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from)
问题与处理策略
1、问题描述
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y)
plt.title("折线图")
plt.xlabel("X 轴")
plt.ylabel("Y 轴")
plt.show()
D:\Python3.10.2\lib\tkinter\__init__.py:839: UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from font(s) DejaVu Sans.
func(*args)
D:\Python3.10.2\lib\tkinter\__init__.py:839: UserWarning: Glyph 31616 (\N{CJK UNIFIED IDEOGRAPH-7B80}) missing from font(s) DejaVu Sans.
func(*args)
D:\Python3.10.2\lib\tkinter\__init__.py:839: UserWarning: Glyph 21333 (\N{CJK UNIFIED IDEOGRAPH-5355}) missing from font(s) DejaVu Sans.
func(*args)
D:\Python3.10.2\lib\tkinter\__init__.py:839: UserWarning: Glyph 25240 (\N{CJK UNIFIED IDEOGRAPH-6298}) missing from font(s) DejaVu Sans.
func(*args)
D:\Python3.10.2\lib\tkinter\__init__.py:839: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from font(s) DejaVu Sans.
func(*args)
D:\Python3.10.2\lib\tkinter\__init__.py:839: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from font(s) DejaVu Sans.
func(*args)
2、问题原因
DejaVu Sans
不支持中文字符导致的3、处理策略
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
# 设置支持中文的字体,使用黑体
plt.rcParams['font.sans-serif'] = ['SimHei']
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y)
plt.title("折线图")
plt.xlabel("X 轴")
plt.ylabel("Y 轴")
plt.show()
作者:我命由我12345