google colab上让 python 视觉化套件 matplotlib 显示中文

 如何在 Colab 上让 python 视觉化套件 matplotlib 显示中文 

  1. 下载开源字体,目前是使用 Google 的 Noto Sans 系列
  2. 讲开源字体下载后自动导入字符型文件夹
  3. 在 matplotlib 设定字符参数
    # 从 Google API 上下载暂存字体放到咱村文件夹下
    !wget 'https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKtc-hinted.zip'
    !mkdir /tmp/fonts
    !unzip -o NotoSansCJKtc-hinted.zip -d /tmp/fonts/
    !mv /tmp/fonts/NotoSansMonoCJKtc-Regular.otf /usr/share/fonts/truetype/NotoSansMonoCJKtc-Regular.otf -f
    !rm -rf /tmp/fonts
    !rm NotoSansCJKtc-hinted.zip
    --2022-06-26 08:47:46--  https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKtc-hinted.zip
    Resolving noto-website-2.storage.googleapis.com (noto-website-2.storage.googleapis.com)... 173.194.213.128, 2607:f8b0:400c:c0a::80
    Connecting to noto-website-2.storage.googleapis.com (noto-website-2.storage.googleapis.com)|173.194.213.128|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 121247366 (116M) [application/zip]
    Saving to: ‘NotoSansCJKtc-hinted.zip’
    
    NotoSansCJKtc-hinte 100%[===================>] 115.63M  39.4MB/s    in 2.9s    
    
    2022-06-26 08:47:49 (39.4 MB/s) - ‘NotoSansCJKtc-hinted.zip’ saved [121247366/121247366]
    
    Archive:  NotoSansCJKtc-hinted.zip
      inflating: /tmp/fonts/LICENSE_OFL.txt  
      inflating: /tmp/fonts/NotoSansCJKtc-Black.otf  
      inflating: /tmp/fonts/NotoSansCJKtc-Bold.otf  
      inflating: /tmp/fonts/NotoSansCJKtc-DemiLight.otf  
      inflating: /tmp/fonts/NotoSansCJKtc-Light.otf  
      inflating: /tmp/fonts/NotoSansCJKtc-Medium.otf  
      inflating: /tmp/fonts/NotoSansCJKtc-Regular.otf  
      inflating: /tmp/fonts/NotoSansCJKtc-Thin.otf  
      inflating: /tmp/fonts/NotoSansMonoCJKtc-Bold.otf  
      inflating: /tmp/fonts/NotoSansMonoCJKtc-Regular.otf  
      inflating: /tmp/fonts/README       
    # 指定字体
    import matplotlib.font_manager as font_manager
    import matplotlib.pyplot as plt
    
    font_dirs = ['/usr/share/fonts/truetype/']
    font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
    
    for font_file in font_files:
      font_manager.fontManager.addfont(font_file)
    
    plt.rcParams['font.family'] = "Noto Sans Mono CJK TC"
    #测试
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
    
    x = np.arange(1, 12)
    y = x ** 2 + 4
    plt.title("Matplotlib demo")
    plt.xlabel("时间(分钟)")
    plt.ylabel("金额($)")
    plt.plot(x,y)
    plt.show()

  4. 显示成功 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值