How to load .ttf file in matplotlib using mpl.rcParams?

后端 未结 3 1117
逝去的感伤
逝去的感伤 2020-12-01 15:10

I have a matplotlib script that starts ...

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

mpl.rcParams[\'xt         


        
3条回答
  •  猫巷女王i
    2020-12-01 15:44

    Specifying a font family:

    If all you know is the path to the ttf, then you can discover the font family name using the get_name method:

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import matplotlib.font_manager as font_manager
    
    path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf'
    prop = font_manager.FontProperties(fname=path)
    mpl.rcParams['font.family'] = prop.get_name()
    fig, ax = plt.subplots()
    ax.set_title('Text in a cool font', size=40)
    plt.show()
    

    Specifying a font by path:

    import matplotlib.pyplot as plt
    import matplotlib.font_manager as font_manager
    
    path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf'
    prop = font_manager.FontProperties(fname=path)
    fig, ax = plt.subplots()
    ax.set_title('Text in a cool font', fontproperties=prop, size=40)
    plt.show()
    

提交回复
热议问题