码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • How to solve matplotlib Chinese garbled characters in Ubuntu 22.04


    conda create -n huizhou python=3.8
    
    • 1
    conda activate huizhou
    
    • 1
    conda install numpy matplotlib
    
    • 1
    pip install mplfonts
    
    • 1
    mplfonts init
    
    • 1
    # 导入必要的库
    import numpy as np
    import matplotlib.pyplot as plt
    
    # 创建角度数组,从0到2π
    x = np.linspace(0, 2 * np.pi, 100)
    
    # 计算正弦、余弦和正切值
    sin_x = np.sin(x)
    cos_x = np.cos(x)
    tan_x = np.tan(x)
    
    # 创建绘图
    fig, axes = plt.subplots(3, 1, figsize=(8, 12))
    
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体(SimHei)字体
    plt.rcParams['axes.unicode_minus'] = False   # 允许在坐标轴上显示负号
    
    # plt.rcParams['font.family'] = 'serif'
    # plt.rcParams['font.sans-serif'] = ['Noto Sans CJK JP']
    # plt.rcParams['axes.unicode_minus'] = False # 允许在坐标轴上显示负号
    
    # plt.rcParams['font.sans-serif'] = ['WenQuanYi Micro Hei']
    # plt.rcParams['axes.unicode_minus'] = False  # 允许在坐标轴上显示负号
    
    # 绘制正弦函数的波形
    axes[0].plot(x, sin_x, label='sin(x)', color='blue')
    axes[0].set_title('正弦函数的波形')
    axes[0].set_xlabel('角度 (弧度)')
    axes[0].set_ylabel('sin(x)')
    axes[0].legend()
    axes[0].grid(True)
    
    # 绘制余弦函数的波形
    axes[1].plot(x, cos_x, label='cos(x)', color='green')
    axes[1].set_title('余弦函数的波形')
    axes[1].set_xlabel('角度 (弧度)')
    axes[1].set_ylabel('cos(x)')
    axes[1].legend()
    axes[1].grid(True)
    
    # 绘制正切函数的波形
    axes[2].plot(x, tan_x, label='tan(x)', color='red')
    axes[2].set_title('正切函数的波形')
    axes[2].set_xlabel('角度 (弧度)')
    axes[2].set_ylabel('tan(x)')
    axes[2].legend()
    axes[2].grid(True)
    axes[2].set_ylim(-10, 10)  # 限制y轴范围以便更好地显示正切函数
    
    # 调整布局
    plt.tight_layout()
    # 显示图形
    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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述

    参考文献

    • github / matplotlibChinesefix
    • How To Install fonts-noto-cjk on Ubuntu 22.04
    • How To Install fonts-noto-cjk-extra on Ubuntu 22.04
    • Why chinese character can’t be displayed after installing noto cjk
    • How to install and manage fonts on Linux
    • 在Ubuntu系统中安装字体(以安装华文行楷和方正舒体为例)
    • 一招解决Ubuntu在英文系统上汉字字形不正确问题
    • pypi / mplfonts
    • github / mplfonts
    • A Guide to Displaying CJK Characters in Matplotlib
  • 相关阅读:
    7-Redis工具类
    sqlserver查询多个分类的最新时间数据
    Javascript Set和Map数据结构
    待看12313132
    Clion+Qt,在运行exe文件时出现黑窗口的解决方案
    机组运行约束对机组节点边际电价的影响研究(Matlab代码实现)
    LLM预训练之RLHF(一):RLHF及其变种
    MetAug: Contrastive Learning via Meta Feature Augmentation
    1074 Reversing Linked List
    openssl: issue-10463: 对/dev/random的依赖
  • 原文地址:https://blog.csdn.net/qwfys200/article/details/138092287
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号