plt.title() function Chinese can not display the problem

Problem Description

Since plt.title() displays English by default when drawing a picture, if we set the title to Chinese, it will not be displayed, as shown in the figure:

plt.title('训练损失')
plt.plot(np.arange(len(losses)), losses, '-o', color='red')
plt.show()

insert image description here

Solution

Add two lines of code before drawing:

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title('训练损失')
plt.plot(np.arange(len(losses)), losses, '-o', color='red')
plt.show()

insert image description here

plt.title() function parameter description

fontsize set the font size, the default is 12, optional parameters ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large']

fontweight set font weight, optional parameters ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']

fontstyle set font type, optional parameter ['normal' | 'italic' | 'oblique'], italic italic, oblique oblique

verticalalignment sets the horizontal alignment, optional parameters: 'center' , 'top' , 'bottom' , 'baseline'

horizontalalignment sets the vertical alignment, optional parameters: left, right, center

rotation (rotation angle) optional parameters are: vertical, horizontal can also be a number

alpha transparency, parameter value between 0 and 1

backgroundcolor title background color

bbox adds a frame to the title, the common parameters are as follows:

boxstyle box shape
facecolor (abbreviated fc) background color
edgecolor (abbreviated ec) border line color
edgewidth border line size

Guess you like

Origin blog.csdn.net/weixin_45080292/article/details/130304191