关于python:matplotlib:如何将Arial-font用于图例和LaTeX-mathfont用于xlabel和ylabel

matplotlib: How to use Arial-font for legend und LaTeX-mathfont for xlabel und ylabel

我想将Arial设置为Legend和图形标题,将LaTeX math-font设置为x-和y-label。

情况1:不使用rcParams['text.usetex'] = True

1
2
3
4
5
6
7
8
9
10
11
import matplotlib
import matplotlib.pyplot as plt
legend_font = {'family' : 'Arial', 'weight' : 'normal', 'size': 23}

a = [1, 2, 3]
b = [1, 2, 3]

plt.plot(a, b, label="Legend")
plt.xlabel(r"$f/MHz$")
plt.legend(prop=legend_font)
plt.show()

enter image description here

图例字体可以是Arial,但是xlabel的字体看起来很难看。

情况2:使用rcParams['text.usetex'] = True

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib
import matplotlib.pyplot as plt

matplotlib.rcParams['text.usetex'] = True


a = [1, 2, 3]
b = [1, 2, 3]

plt.plot(a, b, label="Legend")
plt.xlabel(r"$f/MHz$")
legend_font = {'family' : 'Arial', 'weight' : 'normal', 'size': 23}
plt.legend(prop=legend_font)
plt.show()

>
</p>
<p><center> <script src=

现在,xlabel的字体是正确的。 但是Legend的字体不再起作用。

我该如何解决?

谢谢你提前


在案例2代码(上面编写的第二个代码)的plt.xlabel()之后和legend_font = {'family' : 'Arial', 'weight' : 'normal', 'size': 23}之前添加以下行

1
matplotlib.style.use('classic')

输出量

enter image description here