How to solve matplotlib Chinese garbled characters in Ubuntu 22.04

conda create -n huizhou python=3.8
conda activate huizhou
conda install numpy matplotlib
pip install mplfonts
mplfonts init
# 导入必要的库
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. Ubuntu2204配置samba

    2024-04-23 09:02:05       75 阅读
  2. 2024.Python

    2024-04-23 09:02:05       56 阅读
  3. 12.<span style='color:red;'>2024</span>

    12.2024

    2024-04-23 09:02:05      40 阅读
  4. 2024.4

    2024-04-23 09:02:05       32 阅读
  5. 笔记2024

    2024-04-23 09:02:05       29 阅读
  6. MS<span style='color:red;'>2024</span>

    MS2024

    2024-04-23 09:02:05      26 阅读
  7. css2024

    2024-04-23 09:02:05       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 09:02:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 09:02:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 09:02:05       87 阅读
  4. Python语言-面向对象

    2024-04-23 09:02:05       96 阅读

热门阅读

  1. markdown语法转换成html渲染到页面

    2024-04-23 09:02:05       40 阅读
  2. MongoDB的UTCDateTime如何使用

    2024-04-23 09:02:05       34 阅读
  3. milvus 相似度检索的底层原理

    2024-04-23 09:02:05       35 阅读
  4. 【LeetCode热题100】【图论】腐烂的橘子

    2024-04-23 09:02:05       35 阅读