【Matlab】绘制混淆矩阵示意图+colormap调整方法

主代码

%https://blog.youkuaiyun.com/weixin_42943114/article/details/81811556
%https://blog.youkuaiyun.com/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)以红色为例,系统会自动切换渐变色
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值