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


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

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

    4. 显示成功 

  • 相关阅读:
    第19章 数据库备份与恢复【4.日志与备份篇】【MySQL高级】
    MATLAB配置编译器(包括vs和mingw)
    推荐几个技术学习的网站
    GEE生物量和碳储量——指定研究区利用遥感影像红色波段阈值(大津法)提取森林范围
    Maven - 3、详解maven解决依赖问题
    Docker-可视化管理工具总结-推荐使用Portainer
    vue3-hash-calendar,一款基于vue3.x开发的移动端日期时间选择组件
    《微服务实战》 第二十章 Redis连接指令 客户端指令 服务器指令
    这样封装echarts简单好用
    Java Executors类的9种创建线程池的方法及应用场景分析
  • 原文地址:https://blog.csdn.net/weixin_39645344/article/details/125471772