码农知识堂 - 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绘图中文乱码问题解决

  • 相关阅读:
    接口自动化测试思路和实战(2):模块化测试脚本框架
    一文搞懂V8引擎的垃圾回收机制
    AI视频监控在畜牧养殖中的技术应用解决方案
    【Android从零单排系列九】《Android视图控件——ImageView》
    Maven的安装与配置
    .NET 8 Release Candidate 1 (RC1)现已发布,包括许多针对ASP.NET Core的重要改进!
    Vue3项目练习详细步骤(第三部分:文章分类页面模块)
    redis设计规范
    SpringCloud——注册中心Eureka
    Springboot信息泄露以及heapdump的利用
  • 原文地址:https://blog.csdn.net/weixin_42854929/article/details/133771142
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号