Zuletzt aktiv 1725710320

解决Matplotlib不显示汉字的问题,设置汉语字体

LiuShen's Avatar LiuShen hat die Gist bearbeitet 1725710319. Zu Änderung gehen

1 file changed, 19 insertions

matplotlib-zh-Han.py(Datei erstellt)

@@ -0,0 +1,19 @@
1 + import matplotlib.pyplot as plt
2 + import matplotlib.font_manager as fm
3 +
4 + # 设置中文字体
5 + plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
6 + plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
7 +
8 + # 示例数据
9 + x = [1, 2, 3, 4]
10 + y = [10, 20, 15, 25]
11 +
12 + plt.plot(x, y, label="示例数据")
13 + plt.xlabel("时间")
14 + plt.ylabel("值")
15 + plt.title("示例图表")
16 + plt.legend()
17 +
18 + # 显示图表
19 + plt.show()
Neuer Älter