plt绘图中文显示乱码

plt.xlabel显示乱码

import matplotlib
# 获取本机上的中文字体
zh_font = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simsun.ttc')
plt.xlabel('排名',fontproperties=zh_font)
plt.title('曲线',fontproperties=zh_font)

plt.plot显示乱码

import matplotlib.pyplot as plt 
import numpy as np
plt.rcParams['font.sans-serif']=['SimSun'] #用来正常显示中文标签,宋体
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 #有中文出现的情况,需要u'内容'
x_axis = np.array([ 1,5,10])
plt.plot(x_axis, [0.7,0.8,0.9], 'g-', label='插值',marker='o')

显示异常 findfont

  • findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
    findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
# 通过python找到python字体目录
import matplotlib 
print(matplotlib.matplotlib_fname())
# 输出:cache/matplotlib, 然后删除此目录
# 等会运行python缓存文件就会自动生成
# 如果还没有解决,修改 matplotlibrc
font.family         : sans-serif   
 # 去掉前面的#     
 font.sans-serif     : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif  
 # 去掉前面的#,并在冒号后面添加SimHei
 axes.unicode_minus  : False
 # 去掉前面的#,并将True改为False
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容