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:
Now, I want to colorize these circles with different colors. I could not solve this matter. I appreciate any contribution to my simple code.
Approach 1: using
rectangle
, solid colors. Some circles may be coveredThe 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.
Approach 2: using
patch
, transparent colors. Covered circles are visibleTo 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 thepatch
function. The code is basically like yours, replacingplot
bypatch
and specifying color and transparency as by the appropriate properties.EDIT: I just tested on R2017b and
'none'
doesn't seem to be supported as a color forpatch
anymore. Thus, replace'none'
by'w'
(will get overridden by the subsequent parameters):or alternatively delete
'none'
and'facecolor'
: