graphics - How colorize Circles in a plot in MATLAB? -
i have matlab code follows:
minval = -1; maxval = 1; maxradius = 0.5; ncircles = 5; dimension = 2; circles = zeros(ncircles, dimension); radius = zeros(ncircles, 1); = 1 : ncircles circles(i,:) = unifrnd(minval, maxval, [1, dimension]); radius(i) = unifrnd(0, maxradius, 1); end t = 0 : .1 : 2 * pi; figure; hold on; = 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 circle as:
now, want colorize these circles different colors. not solve matter. appreciate contribution simple code.
approach 1: using rectangle
, solid colors. circles may covered
the simplest (although not intuitive) way plot circles use rectangle
function 'curvature'
property set [1 1]
. have circles filled, specify color via 'facecolor'
property. color of circle border controlled 'edgecolor'
property.
since circles colored, may have of them partly or covered other circles.
modified lines in code marked 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. change `hsv` `jet`, `cool`, ... = 1 : ncircles circles(i,:) = unifrnd(minval, maxval, [1, dimension]); radius(i) = unifrnd(0, maxradius, 1); end figure; hold on; = 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;
approach 2: using patch
, transparent colors. covered circles visible
to make circles visible if have been covered can use colors transparency (alpha). rectangle
not support transparency, have resort patch
function. code yours, replacing plot
patch
, specifying color , transparency 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. change `hsv` `jet`, `cool`, ... alpha = .5; %// define level of transparency = 1 : ncircles circles(i,:) = unifrnd(minval, maxval, [1, dimension]); radius(i) = unifrnd(0, maxradius, 1); end t = 0 : .1 : 2 * pi; figure; hold on; = 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 transparency end axis equal; %// same aspect ratio in both axes grid on;
Comments
Post a Comment