How can I show the legend to all bars?

3 ビュー (過去 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;
  1 件のコメント
Haitham AL Satai
Haitham AL Satai 2022 年 11 月 13 日
@Adam Danz Thank you very much dear, sir.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by