在Windows系统中matplotlib绘图时中文出现乱码

解决方法:

加入代码

设置中文字体显示(如果系统支持)

plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
plt.rcParams['axes.unicode_minus'] = False # 正确显示负号



原始代码:

点击查看代码
import numpy as np
import matplotlib.pyplot as plt
# 设置中文字体显示(如果系统支持)
# plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
# plt.rcParams['axes.unicode_minus'] = False # 正确显示负号
# 模拟数据
user_nums = np.array([10, 50, 100, 200, 300, 400, 500]) # 用户数量
latency = 100 + 0.5 * user_nums + 0.002 * user_nums**2 # 网络延迟(非线性增长)
success_rate = 100 - 0.1 * user_nums - 0.0003 * user_nums**2 # 任务成功率(百分比)
# 绘制双Y轴图表
fig, ax1 = plt.subplots(figsize=(10, 6))
# 左侧Y轴:网络延迟
ax1.plot(user_nums, latency, 'b-o', linewidth=2, markersize=8, label='网络延迟')
ax1.set_xlabel('用户数量', fontsize=12)
ax1.set_ylabel('网络延迟 (ms)', color='b', fontsize=12)
ax1.tick_params(axis='y', labelcolor='b')
ax1.grid(linestyle='--', alpha=0.7)
# 右侧Y轴:任务成功率
ax2 = ax1.twinx()
ax2.plot(user_nums, success_rate, 'r--s', linewidth=2, markersize=8, label='任务成功率')
ax2.set_ylabel('任务成功率 (%)', color='r', fontsize=12)
ax2.tick_params(axis='y', labelcolor='r')
# 标题和图例
plt.title('物联网计算卸载:用户数量对网络延迟与任务成功率的影响', fontsize=14)
fig.legend(loc='upper right', bbox_to_anchor=(0.85, 0.85), fontsize=10)
plt.tight_layout()
plt.show()

绘图:

image





修改后的代码:

点击查看代码
import numpy as np
import matplotlib.pyplot as plt
# 设置中文字体显示(如果系统支持)
plt.rcParams['font.sans-serif'] = ['SimHei'] # Windows系统
plt.rcParams['axes.unicode_minus'] = False # 正确显示负号
# 模拟数据
user_nums = np.array([10, 50, 100, 200, 300, 400, 500]) # 用户数量
latency = 100 + 0.5 * user_nums + 0.002 * user_nums**2 # 网络延迟(非线性增长)
success_rate = 100 - 0.1 * user_nums - 0.0003 * user_nums**2 # 任务成功率(百分比)
# 绘制双Y轴图表
fig, ax1 = plt.subplots(figsize=(10, 6))
# 左侧Y轴:网络延迟
ax1.plot(user_nums, latency, 'b-o', linewidth=2, markersize=8, label='网络延迟')
ax1.set_xlabel('用户数量', fontsize=12)
ax1.set_ylabel('网络延迟 (ms)', color='b', fontsize=12)
ax1.tick_params(axis='y', labelcolor='b')
ax1.grid(linestyle='--', alpha=0.7)
# 右侧Y轴:任务成功率
ax2 = ax1.twinx()
ax2.plot(user_nums, success_rate, 'r--s', linewidth=2, markersize=8, label='任务成功率')
ax2.set_ylabel('任务成功率 (%)', color='r', fontsize=12)
ax2.tick_params(axis='y', labelcolor='r')
# 标题和图例
plt.title('物联网计算卸载:用户数量对网络延迟与任务成功率的影响', fontsize=14)
fig.legend(loc='upper right', bbox_to_anchor=(0.85, 0.85), fontsize=10)
plt.tight_layout()
plt.show()

image



posted on   Angry_Panda  阅读(19)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2024-03-02 新手入门深度学习:在不使用Google的情况下如何在国内获得免费的算力 —— 算力共享,驱动人工智能创新的新引擎
2020-03-02 【转载】 优必选悉尼 AI 研究院何诗怡:基于课程学习的强化多标签图像分类算法 | 分享总结

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示