how to only plot a certain area of an existing plot

7 views (last 30 days)
Hi, so i have this figure which is produced from my code (attached). I would like to reproduce the plot but for it to be masked within a circle and for the data points to only be plotted within this circle. The circle is to have a radius of 0.045m and the centre is to be located at (0,0). I have also attached an image of what kind of thing I am after.
Thank you in advance! I have attached my code.
n = 250; % number of points on the voronoi diagram
L2x = zeros(1,n); % array to store the L2 x values
L2y = zeros(1,n); % array to store the L2 y values
for i=1:n
q = randi([-50 50],1,1); %Parameters for the random case
L2x(i) = q.*1e-3 ; %limit that L2 must be greater than 0.5mm is obeyed
w = randi([-50 50],1,1);
L2y(i) = w.*1e-3;
P = [L2x(:) L2y(:)]; %extracting the x and y values and placing it into one matrix
voronoi(P(:,1),P(:,2)) % Plotting the voronoi diagram
[V,C] = voronoin(P); % storing the voroni vertexs' in V
V(isinf(V)) = 1; % replacing the inf values in the array to 1
for i=1:length(C) %finding the centroids for each cell
Cnew = V(C{i},:);
[xnew,ynew] = centroid(polyshape(Cnew(:,1),Cnew(:,2)));
% m = (x)
hold on
axis ([-0.050 0.050 -0.050 0.050])
hold off

Answers (1)

Sam Chak
Sam Chak on 30 Jun 2022
Generally, this imcrop() command is used to crop image, but it can only crop rectangular shape.
To crop a circular shape out of the image, you need some tricks.
Follow the procedure as detailed by out community's best @Image Analyst. (no pun intended).
  1 Comment
rman on 1 Jul 2022
Im afraid this isnt exactly what im after. I am not after saving the image and cropping the image. Im trying to produce the first plot but write a script that only plots the points within the circle and then doesnt plot anything which is not inside it.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by