How can I show the legend to all bars?

1 vue (au cours des 30 derniers jours)
Haitham AL Satai
Haitham AL Satai le 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;

Réponse acceptée

Cris LaPierre
Cris LaPierre le 13 Nov 2022
Modifié(e) : Cris LaPierre le 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 commentaires
Cris LaPierre
Cris LaPierre le 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 le 13 Nov 2022
@Cris LaPierre Thank you very much dear, sir.

Connectez-vous pour commenter.

Plus de réponses (1)

Adam Danz
Adam Danz le 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;
  1 commentaire
Haitham AL Satai
Haitham AL Satai le 13 Nov 2022
@Adam Danz Thank you very much dear, sir.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by