Successfully solved the problem that the coordinate axis does not display Chinese and only half of the Chinese is displayed during the confusion matrix drawing process! ! !

Question 1 , the coordinate axis does not display Chinese, as shown in the figure above :

Problem 1, solution: After adding the code plt.rcParams['font.sans-serif'] = ['SimSun'] , Chinese is successfully displayed.

 

Problem 2: The Chinese language of the coordinate axis cannot be displayed completely . As shown in the figure above, many methods have been tried, including adjusting the size of the canvas and the font size of the coordinate axis, but none of them can be solved.

Problem 2 Solution: plt.tight_layout()After adding the function, it is successfully resolved. 

Function details:

plt.tight_layout()is a function in Matplotlib, which can automatically adjust the spacing between subplots, axes and titles, making the image more compact and more beautiful. It ignores subgraphs that are marked as invisible or have been deleted. When drawing multiple subplots, you can use this function to adjust the layout so that the overlap or gap between subplots is not too large.

Full code for both questions:

    
    plt.rcParams['font.sans-serif'] = ['SimSun']#解决无法显示中文
    plt.rcParams['axes.unicode_minus'] = False
    plt.xlabel('真实标签',fontsize=10)#绘制混淆矩阵图中的文本标签
    plt.ylabel('预测标签')
    plt.tight_layout()#自动调整子图间距,使图紧凑美观
    # 这里这个savefig是保存图片,如果想把图存在什么地方就改一下下面的路径,然后dpi设一下分辨率即可。
    plt.savefig('./tsne/hunxiaojuzhen_'+str(epoch)+'.png', dpi=350)

Final renderings:

 

Guess you like

Origin blog.csdn.net/weixin_61745097/article/details/130329178