python 画图时显示希腊字母u

1 第一种方法【推荐,简单!!!】

利用转义字符进行表示
常见的希腊字母的转义字符

Alpha: \u0391
Beta: \u0392
Gamma: \u0393
Delta: \u0394
Epsilon: \u0395
Zeta: \u0396
Eta: \u0397
Theta: \u0398
Iota: \u0399
Kappa: \u039A
Lambda: \u039B
Mu: \u039C
Nu: \u039D
Xi: \u039E
Omicron: \u039F
Pi: \u03A0
Rho: \u03A1
Sigma: \u03A3
Tau: \u03A4
Upsilon: \u03A5
Phi: \u03A6
Chi: \u03A7
Psi: \u03A8
Omega: \u03A9
u:\u03BC

mu = '\u03BC'
print(mu)

在这里插入图片描述
matplotlib在绘图时,如果遇到希腊字符,直接使用转义字符,这样的话,输出的所有的字符的字体都是一样的

import matplotlib.pyplot as plt

plt, ax = plt.subplots()
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
ax.plot(x, y)
ax.set_ylabel('\u03BCg/m$^3$')

在这里插入图片描述
从结果图中可以看出,希腊字母u和其他的字母的字体是一样的

2 第二种方法【不太推荐】

利用latex语法进行输出
其实我一开始一直都是用的这种方法,但是一直没有找到如何设置字体

import matplotlib.pyplot as plt

plt, ax = plt.subplots()
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
ax.plot(x, y)
# 特殊字符需要通过$括起来,前面尽量添加r
ax.set_ylabel('\u03BCg/m$^3$')

在这里插入图片描述
从结果图中可以看出,希腊字母u和其他的字母的字体是不一样的,u是斜体。

PS:如果大家有设置u的字体的方法,希望能在评论区教教我,万分感谢!

猜你喜欢

转载自blog.csdn.net/weixin_45913084/article/details/131112527