Search code examples
matlabplotmachine-learningdeep-learningmatlab-figure

Is it possible to subplot confusion matrices?


I'm plotting several confusion matrices using plot_confusion() function and I want to put them in a subplot (2x5 figures), but it does not seem to work.It displays every confusion matrix separately. Are there any restriction for plotting confusion? Thanks!

figure

Subplot(2,1,1);

plotconfusion(targets,outputs,'train');

subplot(2,1,2);

plotconfusion(targets1,outputs1,'test')

Solution

  • You're "not supposed" to do that (the functionality is not included), but you can trick Matlab a little, because at the end of a day it's just an axes object:

    %% First Example Data
    [x,t] = cancer_dataset;
    net = patternnet(10);
    net = train(net,x,t);
    y = net(x);
    
    %// plot
    plotconfusion(t,y)
    
    %// get handle and enable second plöt
    cp1 = gcf;
    cp1.NextPlot = 'new'
    ax1 = findobj(cp1,'Type','Axes')
    
    %% Second Example Data
    [x,t] = cancer_dataset;
    net = patternnet(5);
    net = train(net,2*x,t);
    y = net(x);
    
    %// plot
    plotconfusion(t,y)
    
    %// get handle and enable third plöt
    cp2 = gcf;
    cp2.NextPlot = 'new'
    ax2 = findobj(cp2,'Type','Axes')
    
    %% combine plots
    
    f1 = figure(42)
    f1s1 = subplot(121)
    copyobj(allchild(ax1),f1s1)
    f1s2 = subplot(122)
    copyobj(allchild(ax2),f1s2)
    

    enter image description here

    You loose the labels and titles and may need to adjust the axis, but I guess you're able to do that.