How can I show the legend to all bars?

1 次查看(过去 30 天)
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;

请先登录,再进行评论。

更多回答(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 中查找有关 Printing and Saving 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by