Python Matplotlib 中文字符显示问题: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)
Python Matplotlib 中文字符显示问题:UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from_第1张图片
2、问题原因
  • 这个错误是由于 Matplotlib 使用的字体 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()

你可能感兴趣的:(Python,-,问题清单,python,matplotlib,开发语言,学习,学习方法,windows,后端)