How can I show the legend to all bars?

조회 수: 5 (최근 30일)
Haitham AL Satai
Haitham AL Satai 2022년 11월 13일
댓글: Haitham AL Satai 2022년 11월 13일
Below in the figure, I am trying to show the legend to all bars, but I only get the legend for the first bar. How can I show it to all of them?
figure
CoverageArea = [101.1303,0,114.9316,45.2112,116.5973,95.8953];
BarNames = {'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'};
h = bar(CoverageArea,'stacked');
ylabel('Coverage area (m²)');
xticklabels({'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'})
colors = get(gca(), 'ColorOrder' ); % use whatever colors you want here
set(h, 'FaceColor' , 'flat' , 'CData' ,colors(1:6,:)) % set the bars' colors
hLg = legend(BarNames,'Location','best');
grid on;

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 11월 13일
편집: Cris LaPierre 2022년 11월 13일
Legends label data series, not individual points in that data series. The issue right now is that each bar is part of a single data series. You would have to plot each bar separately for them to be labeled individually in the legend. The plus of this is you no longer need to worry about chaging the color of each bar, as that happens automatically.
figure
CoverageArea = [101.1303,0,114.9316,45.2112,116.5973,95.8953];
for b = 1:length(CoverageArea)
bar(b,CoverageArea(b));
hold on
end
hold off
ylabel('Coverage area (m²)');
xticks(1:b)
xticklabels({'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'})
BarNames = {'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'};
legend(BarNames,'Location','best');
grid on;
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2022년 11월 13일
Legends copy the symbol/color from the data series' format. If you want it to be a line, then you should plot data set 2 as a line.
figure
CoverageArea = [101.1303,0,114.9316,45.2112,116.5973,95.8953];
for b = 1:length(CoverageArea)
if CoverageArea(b)==0
plot(CoverageArea(b),'k-');
else
bar(b,CoverageArea(b));
end
hold on
end
hold off
ylabel('Coverage area (m²)');
xticks(1:b)
xticklabels({'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'})
BarNames = {'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'};
legend(BarNames,'Location','best');
grid on;
Haitham AL Satai
Haitham AL Satai 2022년 11월 13일
@Cris LaPierre Thank you very much dear, sir.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2022년 11월 13일
Your bar() command specifies "stacked" but since your data only defines one level per bar, "stacked" should be removed.
This demo uses a colorbar as a legend. It also uses tiledlayout to manage the axes and colorbar positions.
figure
tcl = tiledlayout(1,1); % Using tiledlayout which manages colorbar positions better than just using axes()
nexttile()
CoverageArea = [101.1303,0,114.9316,45.2112,116.5973,95.8953];
nBars = numel(CoverageArea);
BarNames = {'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'};
h = bar(CoverageArea);
% xticklabels(BarNames)
ylabel('Coverage area (m²)');
barcolors = lines(nBars);
set(h, 'FaceColor', 'flat', 'CData' , barcolors)
grid on;
% Set colormap to the same set of colors used to set the bar colors
colormap(barcolors)
% Add colorbar
cb = colorbar();
clim([0,nBars])
cb.YTick = 0.5 : 1 : nBars;
cb.TickLabels = BarNames;

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by