美文网首页
plt绘图中文显示乱码

plt绘图中文显示乱码

作者: 顾北向南 | 来源:发表于2023-02-23 13:17 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:plt绘图中文显示乱码

          本文链接:https://www.haomeiwen.com/subject/duozkdtx.html