Pie Chart Percentages Of Each Section

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 Commenti

Jack - there is no attachment. Once you have chosen the file, please press the Attach File button.
Thanks Geoff, think its there now

Accedi per commentare.

 Risposta accettata

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 Commenti

Awesome, thanks Geoff. Do you know how to show two figures side by side (50% slit of the screen)
Hi Jack - consider using subplot to place to axes side by side.
I have experimented with this method. Does the 'subplot(m,n,p)' refer to the other graph? If so, how will I identify the graph? For example for the pie chart, what should I include in the brackets to display the chart at the same time of a graph I have also plotted? Its hard to explain my thoughts but I hope it makes sense, thanks for your time.
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.)
That's brilliant, thanks for your time

Accedi per commentare.

Più risposte (0)

Tag

Richiesto:

il 7 Mar 2016

Commentato:

il 7 Mar 2016

Community Treasure Hunt

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

Start Hunting!

Translated by