matplotlib 的最新版本只支持 Python 3.9 及以下版本。
在 matplotlib 中使用 latex 语法,直接使用即可,不必在全局启用,否则会报一个找不到 latex.fmt 的错误。

获取系统支持字体

1
2
3
4
5
6
from matplotlib import pyplot as plt
import matplotlib
a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])

for i in a:
print(i)

听说比较好看的字体:

1
2
3
4
5
6
7
8
goodfont=[
'Adobe Heiti Std',
'Arial Unicode MS',
'DengXian',
'SimHei',
'STKaiti',
'STXihei',
]

SimHei 字体下载链接:Link🔗

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
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import pandas as pd
plt.rcParams['font.family'] = 'DejaVu Serif'
zhfont1 = matplotlib.font_manager.FontProperties(fname="./SimHei.ttf")

data = {
'Voltage (V)': list(np.linspace(0.5 ,80,num=160,endpoint=True)),
'Current (10^-7 A)': [
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.2, 0.6, 1.2, 2.3, 3.5, 5.1, 6.9, 8.4, 10.3,12.0,13.4,15.0,16.4,17.4,18.7,19.9,20.6,21.6,22.2,
22.5,22.7,22.9,23.0,22.9,22.7,22.1,21.7,21.3,21.0,21.0,21.4,22.1,23.1,24.5,26.2,27.8,29.7,31.6,33.3,
34.6,35.8,36.7,37.1,37.2,36.8,36.1,34.8,33.2,31.6,29.5,27.5,26.0,24.7,24.4,25.2,27.1,29.9,32.8,36.5,
40.2,43.7,46.1,48.5,50.3,51.0,51.1,50.3,48.9,46.5,43.3,40.3,36.3,32.3,29.2,26.3,24.8,25.2,27.5,31.4,
36.7,41.3,46.9,52.1,55.9,59.7,62.4,63.9,64.5,64.1,62.8,60.1,56.4,52.6,47.5,41.9,37.3,32.2,28.7,27.4,
28.3,32.0,37.2,42.1,49.4,56.0,61.0,66.5,70.3,73.3,75.7,76.7,76.5,75.1,72.4,69.2,64.5,58.8,53.7,47.3,
41.2,36.2,33.7,33.0,35.3,39.1,45.1,52.1,57.9,64.9,71.3,75.9,80.5,83.9,85.7,86.8,86.5,85.4,82.8,79.1
]
}
df = pd.DataFrame(data)

thresholds = [22.0, 32.5, 43.5, 54.5, 66.0, 78.0]
currents = [23.0, 37.2, 51.1, 64.5, 76.7, 86.8]

plt.figure(figsize=(10, 6))
plt.plot(df['Voltage (V)'], df['Current (10^-7 A)'], label='Current vs. Second Grid Voltage')
for i in range(len(thresholds)):
plt.plot([thresholds[i], thresholds[i]], [0, currents[i]], color='black', linestyle='--')
plt.text(x=thresholds[i]-1, y=-5, s=rf'$U_{i+1}$', color='black', fontsize=12, fontproperties=zhfont1)
plt.text(x=thresholds[i]-5, y=currents[i]+3, s=f'{thresholds[i], currents[i]}', color='black', fontsize=12, fontproperties=zhfont1)

plt.text(x=20,y=60,s=r'I-$U_{G_2K}$曲线',horizontalalignment='center',color='black' ,fontsize=15,fontproperties=zhfont1)
plt.text(x=0,y=100,s='XXX',horizontalalignment='center',color='black' ,fontsize=15,fontproperties=zhfont1)
plt.xlabel(r'$U_{G_2K} (V)$',loc='right')
plt.ylabel(r'$I (10^{-7} A)$',loc='top')
plt.title(r'弗兰克-赫兹实验的 $I-U_{G_2K}$ 曲线',fontsize=22,fontproperties=zhfont1)
plt.legend()
plt.grid(True)

ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.spines["bottom"].set_position(("data",0))
ax.yaxis.set_ticks_position("left")
ax.spines["left"].set_position(("data",0))
plt.savefig('exp.png',dpi=300)
plt.show()