matplotlib无法显示中文的问题
- 开源代码
- 2025-08-23 16:54:01

import matplotlib.pyplot as plt from matplotlib import rcParams # 设置支持中文的字体 rcParams['font.sans-serif'] = ['SimHei'] # 使用SimHei字体 rcParams['axes.unicode_minus'] = False # 正确显示负号 # 数据 k_values = [10, 15, 20, 25, 30] ShapeNetpart = [85.7, 86.2, 86.9, 87.2, 86.8] MAPS = [84.2, 85.7, 87.2, 88.3, 87.5] # 绘制折线图,使用不同的标记和漂亮的颜色 plt.plot(k_values, ShapeNetpart, label='ShapeNetpart', marker='o', linestyle='-', color='dodgerblue') plt.plot(k_values, MAPS, label='MAPS', marker='^', linestyle='-', color='darkorange') # 在图中标注数据点 for i in range(len(k_values)): plt.annotate(f'{ShapeNetpart[i]}', (k_values[i], ShapeNetpart[i]), textcoords="offset points", xytext=(0, 5), ha='center') plt.annotate(f'{MAPS[i]}', (k_values[i], MAPS[i]), textcoords="offset points", xytext=(0, 5), ha='center') # 添加标签和标题 plt.xlabel('k 值') plt.ylabel('Score') # plt.title('Comparison of ShapeNetpart and MAPS') # 设置x轴为整数 plt.xticks(k_values) # 设置y轴最大值为92 plt.ylim(84, 89) # 显示图例,位置设为左上角 plt.legend(loc='upper left') # 保存图像 plt.savefig(r'C:\Users\Administrator\Desktop\飞机图片\K_nums.png') # 显示图表 plt.show()
# 设置支持中文的字体 rcParams['font.sans-serif'] = ['SimHei'] # 使用SimHei字体 rcParams['axes.unicode_minus'] = False # 正确显示负号
matplotlib无法显示中文的问题由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“matplotlib无法显示中文的问题”