How can I show the legend to all bars?

3 Ansichten (letzte 30 Tage)
Haitham AL Satai
Haitham AL Satai am 13 Nov. 2022
Kommentiert: Haitham AL Satai am 13 Nov. 2022
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;

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 13 Nov. 2022
Bearbeitet: Cris LaPierre am 13 Nov. 2022
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 Kommentare
Cris LaPierre
Cris LaPierre am 13 Nov. 2022
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 am 13 Nov. 2022
@Cris LaPierre Thank you very much dear, sir.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Danz
Adam Danz am 13 Nov. 2022
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;

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by