解决在Python的matplotlib.pyplot图表中显示中文

文章目录

  • 错误再现
  • 解决patplotlib.pyplot图标中显示中文只要加上

错误再现

为什么会在x轴上显示不出中文呢,是因为matplotlib不支持中文,如果出现中文会报错,报错如下:
解决在Python的matplotlib.pyplot图表中显示中文_第1张图片

E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 24352 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 19977 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 26446 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 22235 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 29579 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 20116 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 23567 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 26126 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 24352 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 19977 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 26446 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 22235 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 29579 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 20116 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 23567 missing from current font.
  font.set_text(s, 0, flags=flags)
E:\PythonIDES\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:180: RuntimeWarning: Glyph 26126 missing from current font.
  font.set_text(s, 0, flags=flags)

解决patplotlib.pyplot图标中显示中文只要加上

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False

就行了


修改后就是这样了:

解决在Python的matplotlib.pyplot图表中显示中文_第2张图片

你可能感兴趣的:(Python,人工智能)