How can I set the colors in the legend if I show a number of legend elements which is lower thatn the number of plotted functions?

4 ビュー (過去 30 日間)
Dear, I'm trying to do a bar chart as the attached one (and shown below).
As you can see, the bar chart contains, for six different number of k (kNN imputation) the class separation for 22 features.
Then I have: group one with 22 features (for k=5), group two with 22 features (for k=4),... end so on.
You can notice that the color helps identifying features through different ks.
Now the problem is with the legend.
I don't want to repeat feat 1feat2 feat3 .... feat22 for six times, so I would like to have the legen showing feat1,..., feat22 just ones.
I can do it but the problem is that the legend color for feati (where i>1) does not match the bar color. In the legen the color are just blues.
How can I do that? I hope I huntitled.jpgave been clear
  3 件のコメント
Elena Casiraghi
Elena Casiraghi 2019 年 8 月 17 日
Sorry! I forgot to attach it!
In the code the number of features is nFeat,
Y is a matrix with size (numel(kimpute), nFeat)
X = 1: numel(kimpute)
kimpute = [5 4 3 2 1 0]
that is, each row of Y contains nFeat columns (one value for each feature: the golub test for assessing the separation between class c1 and class c2)
here is the code:
figClassSep = figure('units','normalized','outerposition',[0 0 1 1]);
title(['Class separation: ' titles{i}]);
hold on; bb = bar(kimpute, Y, 'histc');
cols = getColorValues(bb);
xlabel('k value for KNN imputation');
ylabel('inter-class separation');
hleg = legend(featuresName,'Location','bestoutside');
chleg = get(hleg,'children');
ax = gca;
ax.XTick = X-0.5;
ax.XTickLabels = kimpute;
ax.XTickLabelRotation = 0;
clear Y;
saveas(figClassSep, [dirSave filesep 'GolubSep_' num2str(i) '.jpg']);
savefig(figClassSep, [dirSave filesep 'GolubSep_' num2str(i) '.fig']);
hold off; clear figClassSep;
Elena Casiraghi
Elena Casiraghi 2019 年 8 月 17 日
results is this ( as you can see legends are blue)untitled.jpg

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

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 17 日
h=bar(rand(10,3))
legend(h(1:3),'1','2','3')
  8 件のコメント
Bruno Luong
Bruno Luong 2019 年 8 月 17 日
Your Y array is of size (6 x 132) and not (6 x 22) as in you description in the first post. So when you put in legend of the first 22 data it's just 1/6th of 132, it's normal they are almost blue, since they are at the begining of the spectrum.
I think the problem is on your data representation, and it seems it is not clear if you know them well, at least in the question the dimensions are somehow mixed up.
Elena Casiraghi
Elena Casiraghi 2019 年 8 月 17 日
Dear Bruno,
sorry but copying generated the mess.
Indeed, my data is 6x66 and feature and featureNameMean is a cell array 1x66.
but anyhow, it's correct now, thanks a lot!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 8 月 17 日
The trick for custom legends is to use
for k = 1:num2show
bh(k) = bar(nan, 'color', BarColors(k, :) ;
end
legend(bh, BarLegends)
Where BarColors is an nx3 array of colors to use and BarLegends is a cell array of character vectors giving the corresponding text.
The way this works is that the nan data points tell bar() not to draw anything, but a graphics handle is created and its properties can be manipulated. You legend() those handles and it puts up the corresponding entry even though there are no visible data points for it.

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by