How do I access the axes handles of a confusion matrix?
5 views (last 30 days)
Show older comments
Hi there, I am trying to plot something similar to the photo below (although clearly with a smaller font size), with the same axes labels (actual/predicted) and axes tick marks (class numbers):
The code I was implementing to generate the plot throws the following error when attempting to access the XLabel property of the Xaxis: 'No appropriate method, property or field'XLabel' for class 'matlab.ui.container.Menu'.
I think I am accessing the axes fields incorrectly using the following code:
figure(1)
plotconfusion(known_labels, predicted_labels)
fh = gcf;
ax = gca;
ax.FontSize = 8;
set(findobj(ax,'type','test'),'fontsize',3);
ah = fh.Children(2);
ah.XLabel.String = 'Actual';
ah.YLabel.String = 'Predicted';
ax.XTickLabel = class_labels;
ax.YTickLabel = class_labels;
title('Confusion Matrix 1', 'Fontsize',14)
set(gcf,'color','w');
hold off
Any help with how to do this correctly would be greatly appreciated. Thanks!
0 Comments
Answers (1)
Adam Danz
on 11 Apr 2019
Edited: Adam Danz
on 11 Apr 2019
You're accessing the XTickLabel field correctly but you're trying to enter a 'matlab.ui.container.Menu' object rather than a cell array of strings (or a numeric vector).
The XTickLabel controls the x tick mark labes. Look at the value of "class_labels" in your code (we don't have access to this). It doesn't specify tick labels.
Also, you could improve how you're getting the axis handle. The plotconfusion() function outputs the figure handle.
fh = plotconfusion(known_labels, predicted_labels);
fh.Children(2).XLabel.String = 'Actual';
fh.Children(2).YLabel.String = 'Predicted';
% Examples of x tick labels (for a matrix with 11 columns)
fh.Children(2).XTickLabel = {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k'};
fh.Children(2).XTickLabel = 200:210
6 Comments
Adam Danz
on 15 Apr 2019
This line below (from your penultimate comment) doesn't appear to be a cell array. Could you copy-paste the variable from the command window? How was this variable created? Maybe you could attach that variable to an mat file so I could look at it. The format of this line below is strange to me.
class_labels = {"01"} {"02"} {"03"} ...
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!