When python draws a picture, the Greek letter u is displayed

1 The first method [recommended, simple! ! !

Use escape characters to represent
Escape characters for common Greek letters

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)

insert image description here
When matplotlib draws, if it encounters a Greek character, use the escape character directly, so that the font of all the characters output is the same

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$')

insert image description here
As can be seen from the result figure, the font of the Greek letter u is the same as that of other letters

2 The second method [not recommended]

Use latex syntax to output
In fact, I have always used this method at the beginning, but I have not found how to set the font

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$')

insert image description here
It can be seen from the result figure that the font of the Greek letter u is different from other letters, and u is italic.

PS: If you have a way to set the font of u, I hope you can teach me in the comment area, thank you very much!

Guess you like

Origin blog.csdn.net/weixin_45913084/article/details/131112527