Pie Chart Percentages Of Each Section
3 views (last 30 days)
Show older comments
Hello everyone I have plotted a pie chart of blast energy, thermal energy and nuclear radiation (see attachment). I want to display the percentage each specific section contributes to the overall energy (% each section of pie contributes to whole pie). Any help to edit the code to this would be greatly appreciated. Thanks.
if true
% code
kt = 'Please enter the yield in kilotonnes you wish to test: ';
kt = input(kt);
Ktjoules=(1*10^12);
TotalEnergy=kt*Ktjoules;
Blastenergy=(kt*Ktjoules)/(100)*(50);
ThermalEnergy=(kt*Ktjoules)/(100)*(35);
NuclearRadiation=(kt*Ktjoules)/(100)*(15);
title('Graph to show Blast Engergy, Thermal Energy and Nuclear Radiation in KiloTonnes')
x = [Blastenergy, ThermalEnergy, NuclearRadiation];
labels = {'Blast Energy','Thermal Energy','Nuclear Radiation'};
figure
pie3(x,labels)
view([-65, 35])
end
2 Comments
Geoff Hayes
on 7 Mar 2016
Jack - there is no attachment. Once you have chosen the file, please press the Attach File button.
Accepted Answer
Geoff Hayes
on 7 Mar 2016
Jack - if you wish to include the pie char percentages for each slice (which will be fixed at 50, 35 and 15 with your above code), then you could do
labels = {sprintf('Blast Energy (%.02f%%)',Blastenergy/TotalEnergy*100),...
sprintf('Thermal Energy (%.02f%%)',ThermalEnergy/TotalEnergy*100),...
sprintf('Nuclear Radiation (%.02f%%)',NuclearRadiation/TotalEnergy*100)};
figure
pie3(x,labels)
title('Graph to show Blast Engergy, Thermal Energy and Nuclear Radiation in KiloTonnes')
view([-65, 35])
Note that the title is called after we have created the figure. Try the above and see what happens!
5 Comments
Geoff Hayes
on 7 Mar 2016
Jack - yes, subplot refers to the other axes within your figure. For example, suppose we run the above code and we want to put the pie chart in the left-most axes. Prior to calling pie3 we do
hLeftAxes = subplot(1,2,1);
pie3(hLeftAxes,x,labels);
title(hLeftAxes,'Graph to show Blast Energy, Thermal Energy and Nuclear Radiation in KiloTonnes');
view(hLeftAxes,[-65, 35]);
The call to subplot returns a handle to the axes. We then pass that handle into pie3 to ensure that the pie chart is drawn there. (Same for title and view - we pass the handle to the axes that we wish to update.)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!