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?

1 visualización (últimos 30 días)
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 comentarios
Elena Casiraghi
Elena Casiraghi el 17 de Ag. de 2019
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;

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 17 de Ag. de 2019
h=bar(rand(10,3))
legend(h(1:3),'1','2','3')
  8 comentarios
Bruno Luong
Bruno Luong el 17 de Ag. de 2019
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 el 17 de Ag. de 2019
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!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 17 de Ag. de 2019
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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by