Legend not working with plot (just have some incorrect colors)

조회 수: 1 (최근 30일)
Alexandra Khoury
Alexandra Khoury 2018년 2월 16일
편집: Les Beckham 2018년 2월 17일
I created the following function called getCircle:
%
function [x,y] = getCircle(c,r) %function of the circle
%c = center; r = radius
% Using this program will enable you to plot circles based on values
% inputted for center and radius
x = r*cosd(0:360)+c(1); % x coordinates
y = r*sind(0:360)+c(2); %y coordinates
end
I am calling the function in this script:
% r = [1, 3, 5]; %radius inputs
c = [6, -6] %center inputs
color = {'g', 'b', 'r'}
for radius = r
for c1 = c
[x,y] = getCircle([c1,6],radius);
plot (x,y, color{(radius + 1)/2})
hold on
end
end
grid on
xlabel('Radius from defined centers', 'Fontsize', 15)
ylabel('Radius from defined centers', 'Fontsize' , 15)
legend ('Rad1', 'Rad3','Rad5')
end
my issue is when i plot it, the legend is not correct. I want my legend to correspond to each circle. Here is how it's coming out right now:
As you can see, Rad3 and Rad5 are not color coded correctly. I need Rad 3 to be blue (corresponding to it's circles) and Rad5 to be red. How would I go about this? Thank you

답변 (1개)

Les Beckham
Les Beckham 2018년 2월 17일
편집: Les Beckham 2018년 2월 17일
If you make the 'for radius = r' loop the inner loop instead of the outer loop, you should get what you want.
What is happening is that you are plotting circles that alternate between the left and right side of your figure (the center (c1) is changing fastest) but the colors are based on the radius. So the first two lines you plot (the small circles) have the same green color. Since your legend only has three entries for the total of six lines (i.e. circles) that you plotted, you get the first three - which are two green lines and a blue one.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by