How to show the legend of zero values as a line?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have below the following example,that shows the bars for the following vectors:
% % For Phi/Psi = +/-10
CoverageArea_mean_10 = [84.4735,21.1779,6.4247,2.1416];
CoverageArea_min_10 = [98.5128,21.1779,6.9007,2.1416];
CoverageArea_max_10 = [70.1963,19.0363,5.9488,2.1416];
% For Phi/Psi = +/-40
CoverageArea_mean_40 = [0,4.5211,2.3795,0];
CoverageArea_min_40 = [92.5640,21.1779,6.9007,2.1416];
CoverageArea_max_40 = [0,0.4759,0.2380,0];
as shown below
The problem is there are some zero values and I need them to be represented as a line on legend. How can I fix it?
%% Plotting the coverage area
x = [15,30,45,60];
figure
COVERAGE = [CoverageArea_min_10;CoverageArea_mean_10;CoverageArea_max_10];
COVERAGEAREA = [COVERAGE(:,1)';COVERAGE(:,2)';COVERAGE(:,3)';COVERAGE(:,4)'];
bar(x,COVERAGEAREA);
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel('Coverage area (m²)');
BarNames = {'min','mean','max'};
legend(BarNames,'Location','best');
grid on;
figure
COVERAGE1 = [CoverageArea_min_40;CoverageArea_mean_40;CoverageArea_max_40];
COVERAGEAREA1 = [COVERAGE1(:,1)';COVERAGE1(:,2)';COVERAGE1(:,3)';COVERAGE1(:,4)'];
bar(x,COVERAGEAREA1);
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel('Coverage area (m²)');
BarNames = {'min','mean','max'};
legend(BarNames,'Location','best');
grid on;
댓글 수: 4
Star Strider
2022년 11월 25일
The zero values in ‘CoverageArea_mean_40’ and ‘CoverageArea_max_40’ are already represented as bars on the legend. What result do you want?
채택된 답변
Manoj Mirge
2023년 2월 23일
Hi,
I couldn’t find any proper way to achieve your desired result and I couldn’t see the ways to convert the box legend to a line legend. But I have come up with a possible workaround for your problem.
In bar graph, if you add line plot on the same axis of a bar graph then you will get line legend on that axis. So, you can give desired colour and legend name to the line legend.
I have tried to achieve desired result for your second graph in the code below:
CoverageArea_mean_40 = [0,4.5211,2.3795,0];
CoverageArea_min_40 = [92.5640,21.1779,6.9007,2.1416];
CoverageArea_max_40 = [0,0.4759,0.2380,0];
x = [15,30,45,60];
figure
COVERAGE1 = [CoverageArea_min_40;CoverageArea_mean_40;CoverageArea_max_40];
COVERAGEAREA1 = [COVERAGE1(:,1)';COVERAGE1(:,2)';COVERAGE1(:,3)';COVERAGE1(:,4)'];
bar(x,COVERAGEAREA1);
xlabel( 'Semi-angle at half power, \Phi_1_/_2 (°)' );
ylabel('Coverage area (m²)');
legend('min','Location','best');% Just add only the min legend because you want it to be box legend.
% You can add plot that will add legend on you bar
hold on;
% And you can manually put colour to your line's legend.
% You will have to manually check the colours of mean and max bar and give
% that colour to line legends. You can find colours of mean and max bar from
% property inspectors of figure.
plot([0],[0],"-","DisplayName",'mean','color',[0.85,0.33,0.10]);
plot([0],[0],"-","DisplayName",'max','color',[0.93,0.69,0.13]);
grid on;
Hope this helps.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!