• 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. 显示成功 

  • 相关阅读:
    DOTA-PEG-麦芽糖 maltose-DOTA 麦芽糖-四氮杂环十二烷四乙酸
    大数据必学Java基础(八十二):基于UDP的网络编程
    如何使用连接器添加数据集?—以HK-Domo为例
    SpringCloudAlibaba Gateway(二)详解-内置Predicate、Filter及自定义Predicate、Filter
    【MySQL架构篇】逻辑架构
    实现Element Select选择器滚动加载
    离散制造业生产域制造资源数据标准化及治理建设思路
    ROS系统下webots安装
    java项目利用线程池,同时执行多个需求,返回数据,加快速度
    Java设计模式总结
  • 原文地址:https://blog.csdn.net/weixin_39645344/article/details/125471772