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

1 vue (au cours des 30 derniers jours)
Alexandra Khoury
Alexandra Khoury le 16 Fév 2018
Modifié(e) : Les Beckham le 17 Fév 2018
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

Réponses (1)

Les Beckham
Les Beckham le 17 Fév 2018
Modifié(e) : Les Beckham le 17 Fév 2018
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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by