码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Colab matplotlib画图如何显示中文字体【图例坐标轴均可显示中文】


    Colab notebook用matplotlib画图中文出现方块:
    在这里插入图片描述
    如何解决这个问题呢?

    • 运行wget -O simhei.ttf "https://www.wfonts.com/download/data/2014/06/01/simhei/chinese.simhei.ttf",安装中文字体,这里装得是SimHei,也就是黑体中文
    • 添加以下两行代码即可
    matplotlib.font_manager.fontManager.addfont('simhei.ttf')
    matplotlib.rc('font', family='SimHei')
    
    • 1
    • 2

    运行结果:
    在这里插入图片描述

    附示例源程序:

    !pip install matplotlib
    
    !wget -O simhei.ttf "https://www.wfonts.com/download/data/2014/06/01/simhei/chinese.simhei.ttf"
    
    import matplotlib.pyplot as plt
    import matplotlib
    import numpy as np
    
    # # 新增字體
    matplotlib.font_manager.fontManager.addfont('simhei.ttf')
    matplotlib.rc('font', family='SimHei')
    
    # 定义数据
    grades = ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级']
    rating = ['中', '良', '优']
    numbers = [
        [5647, 1024, 2160],
        [18301, 2756, 4810],
        [34048, 4326, 7744],
        [47731, 5576, 10514],
        [44271, 3562, 9149],
        [53252, 3564, 13213]
    ]
    
    barWidth = 0.25
    
    # 设置位置
    r1 = np.arange(len(numbers))
    r2 = [x + barWidth for x in r1]
    r3 = [x + barWidth for x in r2]
    
    # 绘制柱形图
    plt.bar(r1, [num[0] for num in numbers], color='b', width=barWidth, edgecolor='grey', label='中')
    plt.bar(r2, [num[1] for num in numbers], color='r', width=barWidth, edgecolor='grey', label='良')
    plt.bar(r3, [num[2] for num in numbers], color='g', width=barWidth, edgecolor='grey', label='优')
    
    # 描述柱形图
    plt.xlabel('年级', fontweight='bold')
    plt.xticks([r + barWidth for r in range(len(numbers))], grades)
    
    # 创建图例
    plt.legend()
    
    # 显示图表
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    参考博文:
    在Colab上讓matplotlib顯示中文
    Colab使用matplotlib和seaborn绘图中文乱码问题解决

  • 相关阅读:
    手把手教你电机FOC控制【一】
    ASM字节码插桩:打印方法名、入参、返回值、方法耗时
    使用MapStruct忽略不映射的属性
    批量将文件名称符合要求的文件自动复制到新文件夹:Python实现
    如何实现车联网的灵活数据采集
    计算机竞赛 题目:基于机器视觉opencv的手势检测 手势识别 算法 - 深度学习 卷积神经网络 opencv python
    微信小程序隐私协议相关接口实际使用方式
    深圳/河南/天津/山东产品经理国际认证NPDP招生简章
    【虹科干货】设计微服务架构的原则
    Python基础_第4章_Python数据序列(容器)
  • 原文地址:https://blog.csdn.net/weixin_42854929/article/details/133771142
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号