如何在标签中绘制带有汉字的图形

新手上路,请多包涵

当我在 Python 3 中绘制带有汉字标签的图形时,它无法正常工作:

截屏 ]

我的代码:

 fig = pd.DataFrame({
    '债券收益率':bond,
    '债券型基金收益率':bondFunds,
    '被动指数型基金收益率':indexFunds,
    '总收益率':ret})
fig.plot()
plt.legend(loc=0)
plt.title('债券收益率',
          fontproperties='SimHei',
          fontsize='xx-large')
plt.grid(True)
plt.axis('tight')

原文由 DachuanZhao 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 513
2 个回答

您需要使用 prop kwag 将字体属性显式传递给 legend 函数:

 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')


资源

原文由 Kartik 发布,翻译遵循 CC BY-SA 3.0 许可协议

尝试这个:

 import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # Or any other Chinese characters

原文由 Raphael Hu 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题