主代码
matlab">%https://blog.csdn.net/weixin_42943114/article/details/81811556
%https://blog.csdn.net/Mark711/article/details/141144280
clc
clear
close all
warning off
%% 原始数据
% 假设groundTruth和predictions是已经定义好的向量
TrueLabels = [1 2 1 3 2 3 1 3 2 1 4 5 1 2 2 2];%
PredictedLabels = [1 1 2 3 2 2 1 3 3 1 4 5 1 2 2 2];%
[C,~] = confusionmat(TrueLabels, PredictedLabels);
%% 绘制混淆矩阵
figure(1)
imagesc(C);
% 显示每个单元格的数值
[numRows, numCols] = size(C);
for i = 1:numRows
for j = 1:numCols
text(j, i, num2str(C(i,j)), 'HorizontalAlignment', 'center', 'Color', 'white',"FontSize",30);%字体大小为30
end
end
colormap('parula');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%colormap可自定义
colorbar%产生colorbar
% 添加标题和轴标签
title('Confusion Matrix');
xlabel('Predicted Labels');
ylabel('True Labels');
set(gca,'xticklabel',{'one ','two','three','four','five'},'XTickLabelRotation',30)%设置横轴和横轴标签大小
set(gca,'yticklabel',{'one','two','three','four','five'})%设置纵轴
set(gca,'xtick',1:5)
set(gca,'ytick',1:5)
% set(gca,'FontSize',14,'Fontname', 'Times New Roman');%设置坐标系的数字大小
ZT=30;%字体30号
set(gca,'FontSize',ZT);
aaa=2;
set(gcf,'unit','centimeters','position',[2 2 16*aaa 9*aaa]);
set(gca,"FontName","Times New Roman","FontSize",ZT,"LineWidth",2);
结果
调整colormap的方法
手动调整
(1)编辑-颜色图
(2)若修改最右侧的颜色,双击最右侧颜色下面的标签
(3)选择一个颜色,点击“确定”即可
(4)以红色为例,系统会自动切换渐变色