How colorize Circles in a plot in MATLAB?

2.4k views Asked by At

I have a MATLAB code as follows:

MinVal = -1;
MaxVal = 1;
MaxRadius = 0.5;
nCircles = 5;
Dimension = 2;
Circles = zeros(nCircles, Dimension);
Radius = zeros(nCircles, 1);

for i = 1 : nCircles
Circles(i,:) = unifrnd(MinVal, MaxVal, [1, Dimension]);
Radius(i) = unifrnd(0, MaxRadius, 1);
end

t = 0 : .1 : 2 * pi;

figure;
hold on;
for i = 1 : nCircles
x = Radius(i) * cos(t) + Circles(i,1);
y = Radius(i) * sin(t) + Circles(i,2);
plot(x,y);
end

axis square;
grid on;

The output is a some circle as:

enter image description here

Now, I want to colorize these circles with different colors. I could not solve this matter. I appreciate any contribution to my simple code.

2

There are 2 answers

0
Luis Mendo On BEST ANSWER

Approach 1: using rectangle, solid colors. Some circles may be covered

The simplest (although not very intuitive) way to plot circles is to use the rectangle function with the 'curvature' property set to [1 1]. To have the circles filled, specify the color via the 'facecolor' property. The color of the circle border is controlled by the 'edgecolor' property.

Since circles are colored, you may have some of them partly or fully covered by other circles.

Modified lines in the code are marked with comments.

MinVal = -1;
MaxVal = 1;
MaxRadius = 0.5;
nCircles = 5;
Dimension = 2;
Circles = zeros(nCircles, Dimension);
Radius = zeros(nCircles, 1);
cmap = hsv(nCircles); %// define colors. You could change `hsv` to `jet`, `cool`, ...

for i = 1 : nCircles
    Circles(i,:) = unifrnd(MinVal, MaxVal, [1, Dimension]);
    Radius(i) = unifrnd(0, MaxRadius, 1);
end

figure;
hold on;
for i = 1 : nCircles
    rectangle('Curvature', [1 1], ...
        'Position', [Circles(i,:)-Radius(i) repmat(2*Radius(i),1,2)], ...
        'facecolor', cmap(i,:), 'edgecolor', 'none') %// plot filled circle
end

axis equal; %// same aspect ratio in both axes
grid on;

enter image description here

Approach 2: using patch, transparent colors. Covered circles are visible

To make circles visible even if they have been covered you can use colors with transparency (alpha). rectangle does not support transparency, so you have to resort to the patch function. The code is basically like yours, replacing plot by patch and specifying color and transparency as by the appropriate properties.

MinVal = -1;
MaxVal = 1;
MaxRadius = 0.5;
nCircles = 5;
Dimension = 2;
Circles = zeros(nCircles, Dimension);
Radius = zeros(nCircles, 1);
cmap = hsv(nCircles); %// define colors. You could change `hsv` to `jet`, `cool`, ...
alpha = .5; %// define level of transparency

for i = 1 : nCircles
    Circles(i,:) = unifrnd(MinVal, MaxVal, [1, Dimension]);
    Radius(i) = unifrnd(0, MaxRadius, 1);
end

t = 0 : .1 : 2 * pi;

figure;
hold on;
for i = 1 : nCircles
    x = Radius(i) * cos(t) + Circles(i,1);
    y = Radius(i) * sin(t) + Circles(i,2);
    patch(x, y, 'none', 'facecolor', cmap(i,:), 'facealpha', alpha, ...
        'edgecolor', 'none'); %// plot filled circle with transparency
end

axis equal; %// same aspect ratio in both axes
grid on;

EDIT: I just tested on R2017b and 'none' doesn't seem to be supported as a color for patch anymore. Thus, replace 'none' by 'w' (will get overridden by the subsequent parameters):

patch(x, y, 'w', 'facecolor', cmap(i,:), 'facealpha', alpha, ...
        'edgecolor', 'none'); %// plot filled circle with transparency`

or alternatively delete 'none' and 'facecolor':

patch(x, y, cmap(i,:), 'facealpha', alpha, ...
        'edgecolor', 'none'); %// plot filled circle with transparency

enter image description here

0
Adiel On

@Luis's answer is for circles with fill colors. I add an answer for the case like in the question, for edge colors only.

You can specify the colors that you want, in the order that you want, and than add it as an argument to the plot command:

figure;
colors={'k','b','r','g','y','c'};
for i = 1 : nCircles
   x = Radius(i) * cos(t) + Circles(i,1);
   y = Radius(i) * sin(t) + Circles(i,2);
   hold on;
   plot(x,y,colors{i});
end