Why legend something wrong
조회 수: 1 (최근 30일)
이전 댓글 표시
I got some sample codes from this
I am not sure why plotting some with wrong legends : (
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/305983/image.png)
clear;clc;close all;
h = [0.05 0.025 0.0125 0.00625 0.003125];
A = [0.0103 0.005 0.0019 0.0121 0.1031];
B = [0.0103 0.0052 0.0026 0.0012 0.0011];
C = [0.0103 0.0052 0.0026 0.0013 0.0011];
% Plot filtered data
figure();
%axis([0 3.5e-3 0 1e-3]);
loglog(h, A,'k-'); % plot
hold on;
scatter(h,A,'k','*');
loglog(h, B,'k--');
scatter(h,B,'k','d');
loglog(h, C,'k:');
scatter(h,C,'k','o');
xlabel('grid space [m]')
ylabel('Error Value');
title('Relation of Error Value and Grid Spacing [m]')
legend('A','B','C');
hold off;
grid on;
댓글 수: 0
채택된 답변
Ameer Hamza
2020년 5월 30일
편집: Ameer Hamza
2020년 5월 30일
No need to call loglog and scatter seperately. loglog() can do both
% clear;clc;close all;
h = [0.05 0.025 0.0125 0.00625 0.003125];
A = [0.0103 0.005 0.0019 0.0121 0.1031];
B = [0.0103 0.0052 0.0026 0.0012 0.0011];
C = [0.0103 0.0052 0.0026 0.0013 0.0011];
% Plot filtered data
figure();
%axis([0 3.5e-3 0 1e-3]);
loglog(h, A,'k-*'); % plot
hold on;
loglog(h, B,'k--d');
loglog(h, C,'k:o');
xlabel('grid space [m]')
ylabel('Error Value');
title('Relation of Error Value and Grid Spacing [m]')
legend('A','B','C');
hold off;
grid on;
댓글 수: 0
추가 답변 (1개)
Alan Stevens
2020년 5월 30일
Instead of
loglog(h, A,'k-'); % plot
hold on;
scatter(h,A,'k','*');
% simply use
loglog(h, A, 'k*-');
% with the same for the others (i.e. no need for a separate scatter plot)
% then legend will work ok.
댓글 수: 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!