How do I create a continuously updating pie chart?
3 views (last 30 days)
Show older comments
Hi, I am having problems adding a pie chart that updates when the functions update. The goal is to have a figure filling out an area (an array, that is being transformed from ones to zeros), as well as a chart that, if possible, continuously updates, preferably with percentage. I would guess adding something like "pie((numel(x)/numel(x))*100)", where x is the amount of zeros? I am very new to Matlab so any tips would be much appreciated! Here is what I have so far:
x = 3.0;
y = 3.0;
theta = 20;
xt = cosd(theta);
grass = [0 0.7 0.2
0.4 0.6 0
0.4 0.2 0];
A = ones(30, 20);
A(:, 1) = 2; %B1 = A(:,1);
A(:,20) = 2; %B2 = A(:,20);
A(30,:) = 2; %B3 = A(30,:);
A(1,:) = 2; %B4 = A(1,:);
%A(2,2) = 0;
imagesc(A)
colormap(grass)
axis equal
while sum(A, 'all') > 192
if A(round(y),round(x)+2) == 2
xt = -cosd(theta);
elseif A(round(y),round(x)-2) == 2
xt = cosd(theta);
end
if A(round(y)+2,round(x)) == 2
theta = -abs(theta);
elseif A(round(y)-2,round(x)) == 2
theta = abs(theta);
end
x = x + xt;
y = y + sind(theta);
A(round(y),round(x+1)) = 0;
A(round(y),round(x-1)) = 0;
A(round(y+1),round(x)) = 0;
A(round(y-1),round(x)) = 0;
A(round(y+1),round(x-1)) = 0;
A(round(y+1),round(x+1)) = 0;
A(round(y-1),round(x-1)) = 0;
A(round(y-1),round(x+1)) = 0;
disp("x = "+x+" y = "+y)
imagesc(A)
axis equal
pause(0.0000001)
end
disp(x+" Done "+y)
0 Comments
Answers (1)
Pavan Guntha
on 20 Oct 2021
Hello Aaron,
You could use pie command to plot a pie chart. In order to update it dynamically, you could include the pie chart functionality into the while loop in your code as follows:
figure(1)
imagesc(A)
colormap(grass)
axis equal
while sum(A, 'all') > 192
if A(round(y),round(x)+2) == 2
xt = -cosd(theta);
elseif A(round(y),round(x)-2) == 2
xt = cosd(theta);
end
if A(round(y)+2,round(x)) == 2
theta = -abs(theta);
elseif A(round(y)-2,round(x)) == 2
theta = abs(theta);
end
x = x + xt;
y = y + sind(theta);
A(round(y),round(x+1)) = 0;
A(round(y),round(x-1)) = 0;
A(round(y+1),round(x)) = 0;
A(round(y-1),round(x)) = 0;
A(round(y+1),round(x-1)) = 0;
A(round(y+1),round(x+1)) = 0;
A(round(y-1),round(x-1)) = 0;
A(round(y-1),round(x+1)) = 0;
disp("x = "+x+" y = "+y)
figure(1)
imagesc(A)
axis equal
% Pie Chart Plotting functionality
figure(2)
pie([sum(A==0, "all") sum(A~=0, "all")], '%.3f%%')
labels = {'Covered Region', 'Uncovered Region'};
legend(labels,"Location","northeast")
pause(0.0000001)
end
The result of the above code is as follows where the data presented in the Pie chart gets updated in every iteration:
Hope this helps!
0 Comments
See Also
Categories
Find more on Pie Charts in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!