-1

I have used the following code to generate my confusion matrix, which I have found it on internet :

    confmat = C;
    labels = {'0', '1', '2', '3', '11' };
    numlabels = size(confmat, 1); % number of labels
    confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);
    imagesc(confpercent);
    Mycolors=[0 0.7 0.4; 1 0.9 0.9 ]
    colormap(flipud(Mycolors));
    textStrings = num2str([confpercent(:)], '%.1f%%\n');
    textStrings = strtrim(cellstr(textStrings));
    [x,y] = meshgrid(1:numlabels);
    hStrings = text(x(:),y(:),textStrings(:), ...
        'HorizontalAlignment','center');
    midValue = mean(get(gca,'CLim'));
    textColors = repmat(confpercent(:) > midValue,1,3);
    set(hStrings,{'Color'},num2cell(textColors,2));
    set(gca,'XTick',1:numlabels,... 'XTickLabel',labels,... 'YTick',1:numlabels,... 'YTickLabel',labels,...  'TickLength',[0 0]);

I have gotten the next matrix Here the one I have obtained

While I want to add vertical lines to my matrix to separate between values so I can get a similar one to the next :

The one I am intersted to get

I could get those vertical lines using pcolor(confusion_matrix) but the percentages are shiffted to the corner of each grid and I have got the next picture : Picture obtained with pcolor

baddy
  • 369
  • 1
  • 3
  • 23
  • 2
    So, you grabbed this complete code of the internet. What did you do yourself, and what did you try? Did you read the documentation on `imagesc()`? I presume you did, so please, enlighten us with what you found and why that did not work for you case. – Adriaan Jan 17 '19 at 12:45
  • Of course I have modified some parts of it to get the displayed format and to get almost the same format given by the classificationLearner. For sure it is impossible to get the same label are used by others and the confusion matrix is calculated by me ! – baddy Jan 17 '19 at 15:32
  • Use "fill" which gives you a colored square with black boundaries, for example for the unit square colored green with black boundaries : "fill([0,1,1,0],[0,0,1,1],'g') – Jean Marie Becker Jan 17 '19 at 21:09

2 Answers2

0

Use another axes object

The classic MATLAB trick when having to deal with different axes properties. Basically we are going to create a new axes object, place it on top of the precious one, then make it transparent (no background color) so we can see what's behind, but we'll keep the grid lines well visible right where we want them.

So immediately after your sample code, add:

ax1 = gca  ; % get handle of initial axes
ax2 = axes ; % create new axe and retrieve handle

lim = [0 numlabels] ; % Prepare X and Y properties
tks = 0:numlabels ;

% superpose the new axe on top, at the same position
set(ax2,'Position', get(ax1,'Position') );

% make it transparent (no color)
set(ax2,'Color','none') ;

% set the X and Y properties
set(ax2, ...
    'XLim',lim,'XTick',tks,'XTickLabel','' ,...
    'YLim',lim,'YTick',tks,'YTickLabel','' ) ;

% now set your grid properties
set(ax2,'GridColor','k','GridAlpha',1)

This will get you (data differ a bit because I randomly generated the confusion matrix):

enter image description here

Of course now you have full control over your grid lines, so you can also refine how they appear through the grid properties of the axes. A few interesting properties are:

  • GridLineStyle — Line style for grid lines
  • GridColor — Color of grid lines
  • GridAlpha — Grid-line transparency
  • LineWidth — Line width

For more details, look at the documentation for Axes Properties

Hoki
  • 11,637
  • 1
  • 24
  • 43
  • Does not work I think the problem with using imagesc() ! – baddy Jan 21 '19 at 07:19
  • I used `imagesc()` too, did you try my code after the exact same one you posted in your question? And when you say _doesn't work_, what exactly does not (wrong display results, error...)? – Hoki Jan 22 '19 at 06:41
  • Yes but it gave me the same results may do I need to delete some setting from my code – baddy Jan 23 '19 at 07:31
0

Well ! I have found the requested thing to get the vertical and horizantal line, which is adding the lines simply using plot and hold on :

I have used the next code at the end of the mentioned one in my question :

hold on
plot ([2 2],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot ([1 1],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot ([3 3],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot ([4 4],get(gca, 'YLim'), 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)

plot (get(gca, 'XLim'), [1 1], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [2 2], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [3 3], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)
plot (get(gca, 'XLim'), [4 4], 'Color', [0.15, 0.15, 0.15],'LineWidth',0.5)

I have used 1, 2, 3 and 4 because I have four classes and at the end of each class prediction results I need to plot the line. Hope that will be useful

baddy
  • 369
  • 1
  • 3
  • 23