![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673303/image.jpeg)
Legend in bar plot
401 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
René Lampert
am 3 Jul. 2021
Beantwortet: Greg LANGE
am 19 Okt. 2022
Hello,
I wanna create a legend for a bar plot but I always get a warning message and it only shows one entitie of the legend. Here is a minimal code example
X=categorical({'small','medium','large'});
X=reordercats(X,{'small','medium','large'});
Y=[5 18 56];
TestL={'Mon','Tue','Wed'};
figure()
b=bar(X,Y);
b.FaceColor = 'flat';
b.CData(1,:)=[1 0 0];
b.CData(2,:)=[0 1 0];
b.CData(3,:)=[0 0 1];
legend(TestL)
And this is what i get from Matlab
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673203/image.jpeg)
What is wrong here ? I´m totally confused.
Thanks
0 Kommentare
Akzeptierte Antwort
dpb
am 3 Jul. 2021
Bearbeitet: dpb
am 3 Jul. 2021
A somewhat different approach to Walter's to generate the three needed bar handles -- use a 'stacked' plot with the elements on the diagonal, zero for the off-diagonal elements. 'stacked' creates a bar for each row whose heights are each the sums of the elements on the row. (And, need 'stacked' because otherwise passing an array to bar causes it to draw a 'grouped' bar.)
hB=bar(X,diag(Y,0),'stacked');
hLg=legend(TestL,'Location','northwest');
with default color produces
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673303/image.jpeg)
The bars can be customized as desired through the array of bar handles hB.
3 Kommentare
dpb
am 3 Jul. 2021
set(hB,{'FaceColor'},{'r';'g';'b'})
hLg=legend(TestL,'Location','best')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673338/image.jpeg)
will put where is least conflict with data -- if we do the above but swap the order to
hB=bar(flip(X),fliplr(diag(Y,0)),'stacked');
hLg=legend(TestL,'Location','best')
we get
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/673343/image.jpeg)
where the legend has been moved automagically to the NE corner instead.
As an aside, I don't follow why this isn't the default legend behavior...
Weitere Antworten (2)
Walter Roberson
am 3 Jul. 2021
legend() creates at most one entry for each graphics object. However, each call to bar() creates one graphics object, not one object for each group.
Create one extra bar() object for each item you want to legend(), and use nan as the data for that. legend() on those handles
X=categorical({'small','medium','large'});
X=reordercats(X,{'small','medium','large'});
Y=[5 18 56];
TestL={'Mon','Tue','Wed'};
figure()
b=bar(X,Y);
hold on
b.FaceColor = 'flat';
b.CData(1,:)=[1 0 0];
b.CData(2,:)=[0 1 0];
b.CData(3,:)=[0 0 1];
bh(1) = bar(nan,nan,'r');
bh(2) = bar(nan,nan,'g');
bh(3) = bar(nan,nan,'b');
legend(bh, string(X))
2 Kommentare
Walter Roberson
am 3 Jul. 2021
X=categorical({'small','medium','large'});
X=reordercats(X,{'small','medium','large'});
Y=[5 18 56];
TestL={'Mon','Tue','Wed'};
figure()
b=bar(X,Y);
hold on
b.FaceColor = 'flat';
b.CData(1,:)=[1 0 0];
b.CData(2,:)=[0 1 0];
b.CData(3,:)=[0 0 1];
bh(1) = bar(nan,nan,'r');
bh(2) = bar(nan,nan,'g');
bh(3) = bar(nan,nan,'b');
legend(bh, string(X), 'location', 'northwest')
Greg LANGE
am 19 Okt. 2022
What should I change to have my "Tuesday" to have the same color as my bar ? (purple)
What should I do to have my label as my picture with coordonates ?
Thank you in advance
figure (5)
%modifié
X=categorical({'small','medium','large'});
X=reordercats(X,{'small','medium','large'});
Y=[5 18 56];
TestL={'Mon','Tue','Wed'};
figure()
b=bar(X,Y);
hold on
b.FaceColor = 'flat';
b.CData(1,:)=[1 0 0];
%b.CData(2,:)=[0 1 0];
b.CData(2,:)=[0.4940 0.1840 0.5560];
b.CData(3,:)=[0 0 1];
bh(1) = bar(nan,nan,'r');
bh(2) = bar(nan,nan);%
bh(3) = bar(nan,nan,'b');
bh(2).CData=[0.4940 0.1840 0.5560];
legend(bh, TestL, 'location', 'best') %'northwest'
%legent at the top
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
ylabel('Energie [kWh]')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!