Why legend something wrong

1 Ansicht (letzte 30 Tage)
Michael Wang
Michael Wang am 30 Mai 2020
Beantwortet: Alan Stevens am 30 Mai 2020
I got some sample codes from this
I am not sure why plotting some with wrong legends : (
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;

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 30 Mai 2020
Bearbeitet: Ameer Hamza am 30 Mai 2020
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;

Weitere Antworten (1)

Alan Stevens
Alan Stevens am 30 Mai 2020
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.

Community Treasure Hunt

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

Start Hunting!

Translated by