1. 画图的效果:
2. 代码
function draw_cm(mat, tick)
%%
% 参数:mat-矩阵;tick-要在坐标轴上显示的label向量,例如{'label_1','label_2'...}
%
%%
imagesc(mat); %# 绘彩色图
colormap(flipud(hot));
colorbar;
num_class = size(mat,1);
midValue = mean(get(gca, 'CLim' ));
[x,y] = meshgrid(0:num_class+1);
%% 画中间的虚线
a = 180;
for i=1:num_class+2
hold on
plot(x(i, :), y(i, :), 'Color', [a a a]/255, 'LineStyle', '--'); % '--'
plot(y(i, :), x(i, :), 'Color', [a a a]/255, 'LineStyle', '--');
end
%% 写字
for i=1:num_class
for j=1:num_class
if not (isnan(mat(i, j)))
textStrings = num2str(mat(i, j), '%0.1f');
if mat(i, j) < 1
textStrings = textStrings(2: 3);
elseif mat(i, j) > 90
textStrings = textStrings(2: 4);
else
textStrings = textStrings;
end
textStrings = strtrim(cellstr(textStrings));
hStrings = text(j,i,textStrings(:), 'HorizontalAlignment' , 'center', 'FontSize', 11, 'FontWeight', 'normal' );
textColors = repmat(mat(i, j) > midValue,1,3);
%改变test的颜色,在黑cell里显示白色
set(hStrings,{ 'Color' },num2cell(textColors,2)); %# Change the text colors
end
end
end
%% 设置X/Y轴备注
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24])
yticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24])
xticklabels({'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', 'X', 'Y'})
% set(gca, 'xticklabel' ,{'asas', 'sdawda', 'das', 'dsa', 'das', 'das'}, 'XAxisLocation' , 'bottom' );
rotateXLabels(gca, 45 );
set(gca, 'yticklabel' ,tick, 'fontsize', 12);
xlabel('Predicted Label')
ylabel('True Label')
附:
1. Matlab包含的colormap
http://matlab.izmiran.ru/help/techdoc/ref/colormap.html

2. 字体属性
http://www.ece.northwestern.edu/local-apps/matlabhelp/techdoc/ref/text.html
版权声明:本文为zhangjipinggom原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。