MATLAB Answers

Legends not matching colors

1 view (last 30 days)
Sachin Motwani
Sachin Motwani on 29 Oct 2020
Answered: Kelly Kearney on 29 Oct 2020
My legends are not matching the colors I labeled.
Yu = rand(1000, 1);
Yn = randn(1000, 1);
plot(Yn,'g'), hold on, plot(Yu, 'r'), title('Random noise over time'), legend('Normal',"Uniform");

  2 Comments

Andrew Newell
Andrew Newell on 29 Oct 2020
But they do match: The uniform distribution is Yu and it is plotted with a red line.
Sachin Motwani
Sachin Motwani on 29 Oct 2020
But what about green which is displayed as blue.

Sign in to comment.

Answers (2)

VBBV
VBBV on 29 Oct 2020
Edited: VBBV on 29 Oct 2020
% if true
% code
%end
Yu = rand(1000, 1);
Yn = randn(1000, 1);
plot(Yn,'g'), hold on, plot(Yu, 'r'), title('Random noise over time'), legend('Normal','Uniform');% place..
Place each of the legend text in single quote as ' '. You used a " " for second text

  0 Comments

Sign in to comment.


Kelly Kearney
Kelly Kearney on 29 Oct 2020
Are you certain that those are the only plotting commands? Did you perhaps test plot once with hold on, then call your commands again? Unless you specify handles, the legend command will just label objects in plotting order.
Your code should work fine:
Yu = rand(1000, 1);
Yn = randn(1000, 1);
figure;
plot(Yn,'g');
hold on;
plot(Yu, 'r');
title('Random noise over time');
legend('Normal','Uniform');% place..
But if perhaps you added some extra plotting...
figure;
hold on;
plot(Yn); plot(Yu); % plotting once
plot(Yn,'g'); % and twice
hold on;
plot(Yu, 'r');
title('Random noise over time');
legend('Normal','Uniform');% place..

  0 Comments

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!