Matplotlib Axis Labels and Title
We can use the xlabel() and ylabel() methods to set the labels for the x-axis and y-axis, respectively.
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 output is displayed 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("tutorialpro TEST TITLE")
plt.xlabel("x - label")
plt.ylabel("y - label")
plt.show()
The output is displayed as follows:
Displaying Chinese in Figures
Matplotlib does not support Chinese by default. We can use the following simple method to resolve this.
Here, we use Source Han Sans, a free font developed by Adobe and Google.
Official website: 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, select one of the fonts:
You can also download from Baidu Netdisk: https://pan.baidu.com/s/10-w1JbXZSnx3Tm6uGpPGOw, extraction code: yxqu
.
Download an OTF font, such as SourceHanSansSC-Bold.otf, and place the file in the directory of the 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 to SourceHanSansSC-Bold.otf
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf")
x = np.arange(1,11)
y = 2 * x + 5
plt.title("tutorialpro.org - Test", fontproperties=zhfont1)
# fontproperties to display Chinese, fontsize to set font size
plt.xlabel("x-axis", fontproperties=zhfont1)
plt.ylabel("y-axis", fontproperties=zhfont1)
plt.plot(x,y)
plt.show()
The output is displayed as follows:
Additionally, we can 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)
This prints all registered names from your font_manager's ttflist. Choose a Chinese font, such as STFangsong (Fangsong), and add the following code:
plt.rcParams['font.family'] = ['STFangsong']
We can also customize font styles:
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 to SourceHanSansSC-Bold.otf, size parameter sets 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 be used to set font styles with CSS
plt.title("tutorialpro.org - Test", fontproperties=zhfont1, fontdict=font1)
# fontproperties to display Chinese, fontsize to set font size
plt.xlabel("x-axis", fontproperties=zhfont1)
plt.ylabel("y-axis", fontproperties=zhfont1)
plt.plot(x,y)
plt.show()
The output is displayed as follows:
Positioning Titles and Labels
The title() method provides a loc
parameter to set the title's position, which can be set to: 'left', 'right', and 'center', with 'center' as the default.
The xlabel() method provides a loc
parameter to set the x-axis label's position, which can be set to: 'left', 'right', and 'center', with 'center' as the default.
The ylabel() method provides a loc
parameter to set the y-axis label's position, which can be set to: 'bottom', 'top', and 'center', with 'center' as the default.
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 to SourceHanSansSC-Bold.otf, size parameter sets 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 be used to set font styles with CSS
plt.title("tutorialpro.org - Test", fontproperties=zhfont1, fontdict=font1, loc="left")
# fontproperties to display Chinese, fontsize to set 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 displayed as follows: