How to plot a figure with Chinese Characters in label

后端 未结 1 1096
夕颜
夕颜 2021-01-07 08:55

When I draw a figure with Chinese Character label in Python 3, it doesn\'t work correctly:

\"Screenshot\"]

相关标签:
1条回答
  • 2021-01-07 09:26

    You need to explicitly pass the font properties to legend function using the prop kwag:

    from matplotlib import font_manager
    
    fontP = font_manager.FontProperties()
    fontP.set_family('SimHei')
    fontP.set_size(14)
    
    fig = pd.DataFrame({
        '债券收益率':bond,
        '债券型基金收益率':bondFunds,
        '被动指数型基金收益率':indexFunds,
        '总收益率':ret})
    fig.plot()
    
    # Note the next lines
    plt.legend(loc=0, prop=fontP)
    plt.title('债券收益率', fontproperties=fontP)
    
    plt.grid(True)
    plt.axis('tight')
    

    Source

    0 讨论(0)
提交回复
热议问题