解决matplotlib中文乱码最简单方案

解决matplotlib中文乱码问题方案众多,我认为如下方案是最简单的一个。

1、从电脑中搜索simhei字体,如下示意图是mac检索结果,或者直接搜索simhei.ttf下载字体

拷贝到指定路径:/path/to/mex/simhei.ttf 

2、matplotlib 加载字体

def plot_with_chinese():
    import matplotlib.pyplot as plt
    import matplotlib.font_manager as fm
    fm.fontManager.addfont('/path/to/mex/simhei.ttf')
    font_size=12  
    # plt.rcParams['font.family'] = ['sans-serif']
    plt.rcParams['font.sans-serif'] = ['SimHei'] #散点图标签可以显示中文

    plt.figure(figsize=(13, 9))
    # 使用内置字体名称初始化
    plt.text(0.5, 0.5, 'this is 文本')
    # 验证font_properties参数
    plt.annotate('this is 注解', (0.1, 0.1))
    plt.plot([0,1],[1,0],label='this is图例' )
    # 验证fontproperties参数
    plt.title("this is 标题")
    plt.xlabel("this is x轴")
    plt.ylabel("this is y轴")
    plt.legend()  # 显示图例
    plt.grid()
    plt.show()
plot_with_chinese()

效果如下

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mtj66

看心情

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值