How to show the legend of zero values as a line?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Haitham AL Satai
am 25 Nov. 2022
Kommentiert: Haitham AL Satai
am 23 Feb. 2023
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1206963/image.png)
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 Kommentare
Star Strider
am 25 Nov. 2022
The zero values in ‘CoverageArea_mean_40’ and ‘CoverageArea_max_40’ are already represented as bars on the legend. What result do you want?
Akzeptierte Antwort
Manoj Mirge
am 23 Feb. 2023
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!