MATLAB Answers

Legend option and numbering in boxes

2 ビュー (過去 30 日間)
MINATI PATRA
MINATI PATRA 2021 年 7 月 23 日
コメント済み: MINATI PATRA 2021 年 7 月 24 日
y1 = [85 83 82 87; 81 79 84 86; 82 81 89 87; 89 85 87 80; 90 91 85 87];
figure(1), bar(y1,'stacked'); labels = arrayfun(@(value) [num2str(value,'%2.2f'),'%'],y1,'UniformOutput',false);
xticklabels({'NB','BLR','MLP','J48','CART'}); ylabel('\bfAccuracy in %','color','b');
ax = gca;darkGreen = [1,0,1]; ax.XColor = darkGreen; ax.XAxis.FontSize = 10; ax.YAxis.FontSize = 10; ax.FontWeight = 'bold'; hold off
legend({'2500 Dataset','5000 Dataset','7500 Dataset','10000 Dataset'}),legend boxoff
%% I need legend in 2X2 lines (i.e., 2 Rows & 2 Columns) AND values of y1 should be labled inside the corresponding box

採用された回答

Simon Chan
Simon Chan 2021 年 7 月 23 日
Try the following:
y1 = [85 83 82 87; 81 79 84 86; 82 81 89 87; 89 85 87 80; 90 91 85 87];
figure(1),
b = bar(y1,'stacked');
labels = arrayfun(@(value) [num2str(value,'%2.2f'),'%'],y1,'UniformOutput',false);
xticklabels({'NB','BLR','MLP','J48','CART'});
ylabel('\bfAccuracy in %','color','b');
%
for k = 1:4
xtips1 = b(k).XEndPoints;
ytips1 = b(k).YEndPoints;
labels1 = string(b(k).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','cap')
end
ax = gca;
darkGreen = [1,0,1];
ax.XColor = darkGreen;
ax.XAxis.FontSize = 10;
ax.YAxis.FontSize = 10;
ax.FontWeight = 'bold';
hold off
legend({'2500 Dataset','5000 Dataset','7500 Dataset','10000 Da,taset'},'NumColumns',2),legend boxoff
  1 件のコメント
MINATI PATRA
MINATI PATRA 2021 年 7 月 24 日
Dear Simon
Good work
But it is not working in 2016b version perhaps.
Any modification can conclude my work.

サインインしてコメントする。

その他の回答 (1 件)

Yazan
Yazan 2021 年 7 月 23 日
You can define the number of columns in the legend object using the property NumColumns. Use the following:
legend({'2500 Dataset','5000 Dataset','7500 Dataset','10000 Dataset'}, 'NumColumns', 2, 'Box', 'off')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by