zoukankan      html  css  js  c++  java
  • 设置matplotlib画图支持中文显示

    1.安装中文字体

    git clone https://github.com/tracyone/program_font && cd program_font && ./install.sh
    

    2.拷贝ttf字体文件到matplotlib字体库

    cp /usr/share/fonts/MyFonts/*.ttf /usr/share/matplotlib/mpl-data/fonts/ttf/
    

    3.删除matplotlib缓存列表

    rm -r ~/.cache/matplotlib
    

    4.设置中文代码

    #画图前调用set_matplot_zh_font()即可显示中文
    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    from matplotlib.font_manager import FontManager
    from pylab import mpl
    import subprocess
    def get_matplot_zh_font():
        fm = FontManager()
        mat_fonts = set(f.name for f in fm.ttflist)
        output = subprocess.check_output('fc-list :lang=zh -f "%{family}
    "',sh$
        output = output.decode('utf-8')
        zh_fonts = set(f.split(',',1)[0] for f in output.split('
    '))
        available = list(mat_fonts & zh_fonts)
    
        print '*' * 10,'可用字体','*' * 10
        for f in available:
    		print f
        return available
    def set_matplot_zh_font():
        available = get_matplot_zh_font()
        if len(available) > 0:
                mpl.rcParams['font.sans-serif'] = [available[0]]
                mpl.rcParams['axes.unicode_minus'] = False
  • 相关阅读:
    Algorand算法实现(一)
    什么是跨链?为什么要跨链?
    Web配置简单记录(多更新)
    Web前端常用调优
    回文素数
    postman接口请求快速新建
    两台虚拟机如何ping通
    vue动态改变标题
    js正则匹配版本号V1.0.0
    js获取年月日星期时分秒
  • 原文地址:https://www.cnblogs.com/spjy/p/7086790.html
Copyright © 2011-2022 走看看