Matplotlib axis labels and titles
We can use the xlabel() and ylabel() method to set the labels of the x-axis and y-axis.
Example
import
numpy
as
np
import
matplotlib.
pyplot
as
plt
x
=
np.
array
(
[
1
,
2
,
3
,
4
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
]
)
plt.
plot
(
x
,
y
)
plt.
xlabel
(
"x - label"
)
plt.
ylabel
(
"y - label"
)
plt.
show
(
)
The display result is as follows:

Title
We can use the title() method to set the title.
Example
import
numpy
as
np
import
matplotlib.
pyplot
as
plt
x
=
np.
array
(
[
1
,
2
,
3
,
4
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
]
)
plt.
plot
(
x
,
y
)
plt.
title
(
"RUNOOB TEST TITLE"
)
plt.
xlabel
(
"x - label"
)
plt.
ylabel
(
"y - label"
)
plt.
show
(
)
The display result is as follows:

Displaying Chinese in plots
Matplotlib does not support Chinese by default; we can solve this with the following simple method.
Here we use Source Han Sans, an open-source font released by Adobe and Google.
Official site:https://source.typekit.com/source-han-serif/cn/
GitHub address:https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese
After opening the link, just pick one from there:

You can also download from cloud storage: https://pan.baidu.com/s/10-w1JbXZSnx3Tm6uGpPGOw, extraction code: yxqu.
You can download an OTF font, such as SourceHanSansSC-Bold.otf, and place the file in the directory of the code being executed:
Place the SourceHanSansSC-Bold.otf file in the directory of the currently executing code file:
Example
import
numpy
as
np
from
matplotlib
import
pyplot
as
plt
import
matplotlib
fname is the path to the font library you downloaded; note the path of the SourceHanSansSC-Bold.otf font
zhfont1
=
matplotlib
.
font_manager
.
FontProperties
(
fname
=
"
SourceHanSansSC-Bold.otf
"
)
x
=
np
.
arange
(
1
,
11
)
y
=
2
x
5
plt
.
title
(
"
Runoob Tutorial - Test
"
,
fontproperties
=
zhfont1
)
fontproperties sets Chinese display, fontsize sets the font size
plt
.
xlabel
(
"
x-axis
"
,
fontproperties
=
zhfont1
)
plt
.
ylabel
(
"
y-axis
"
,
fontproperties
=
zhfont1
)
plt
.
plot
(
x
,
y
)
plt
.
show
(
)
The output is shown in the following figure:

In addition, we can also use system fonts:
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)
Print all registered names in your font_manager's ttflist, find a Chinese font you like, for example: STFangsong (Fangsong), Heiti TC (Heiti), then add the following code:
plt.rcParams['font.family']=['STFangsong']
In addition, we can customize the font style:
Example
import
numpy
as
np
from
matplotlib
import
pyplot
as
plt
import
matplotlib
fname is the path to the font file you downloaded, note the path of the SourceHanSansSC-Bold.otf font, and the size parameter sets the font size
zhfont1
=
matplotlib.
font_manager
.
FontProperties
(
fname
=
"SourceHanSansSC-Bold.otf"
,
size
=
18
)
font1
=
{
'color'
:
'blue'
,
'size'
:
20
}
font2
=
{
'color'
:
'darkred'
,
'size'
:
15
}
x
=
np.
arange
(
1
,
11
)
y
=
2
* x +
5
fontdict can use css to set the font style
plt.
title
(
"Runoob Tutorial - Test"
,
fontproperties
=
zhfont1
,
fontdict
=
font1
)
fontproperties sets Chinese display, fontsize sets the font size
plt.
xlabel
(
"x-axis"
,
fontproperties
=
zhfont1
)
plt.
ylabel
(
"y-axis"
,
fontproperties
=
zhfont1
)
plt.
plot
(
x
,
y
)
plt.
show
(
)
The output is as follows:

Positioning of titles and labels
title() method provides the loc parameter to set the position where the title is displayed, which can be set to: ** 'left', 'right', and 'center', with the default value 'center'**.
xlabel() method provides the loc parameter to set the position where the x-axis is displayed, which can be set to: 'left', 'right', and 'center', with the default value 'center'。
ylabel() method provides the loc parameter to set the position where the y-axis is displayed, which can be set to: 'bottom', 'top', and 'center', with the default value 'center'。
Example
import
numpy
as
np
from
matplotlib
import
pyplot
as
plt
import
matplotlib
fname is the path to the font file you downloaded, note the path of the SourceHanSansSC-Bold.otf font, and the size parameter sets the font size
zhfont1
=
matplotlib.
font_manager
.
FontProperties
(
fname
=
"SourceHanSansSC-Bold.otf"
,
size
=
18
)
font1
=
{
'color'
:
'blue'
,
'size'
:
20
}
font2
=
{
'color'
:
'darkred'
,
'size'
:
15
}
x
=
np.
arange
(
1
,
11
)
y
=
2
* x +
5
fontdict can use css to set the font style
plt.
title
(
"Runoob Tutorial - Test"
,
fontproperties
=
zhfont1
,
fontdict
=
font1
,
loc
=
"left"
)
fontproperties sets Chinese display, fontsize sets the font size
plt.
xlabel
(
"x-axis"
,
fontproperties
=
zhfont1
,
loc
=
"left"
)
plt.
ylabel
(
"y-axis"
,
fontproperties
=
zhfont1
,
loc
=
"top"
)
plt.
plot
(
x
,
y
)
plt.
show
(
)
The output is as follows:
