How to display the actual values of my percentages on a pie chart

56 ビュー (過去 30 日間)
LeoAiE
LeoAiE 2021 年 4 月 20 日
コメント済み: LeoAiE 2021 年 4 月 21 日
Hello everyone, this is a basic question for many of you and I would really appreciate if you can take the time and explain to me: How to display the actual values of my percentages on a pie chart and How to display percentage and labels on the pie chart.
TotalNumberOfCars = 75;
White = 34;
Black = 19;
Red = 12;
Silver = 2;
Grey = 3;
Other = 5;
% simple percentage
Per_White = White/ TotalNumberOfCars * 100
Per_Black = Black / TotalNumberOfCars * 100
Per_Red = Red / TotalNumberOfCars * 100
Per_Silver = Silver / TotalNumberOfCars * 100
Per_Grey = Grey / TotalNumberOfCars * 100
per_Other = Other/TotalNumberOfCars * 100
% The results 45.333, 25.333, 16, 2.666, 4
%I missed up somewhere
X = [34 19 12 2 3 5];
pie (X)
Labels = { 'White' , 'Black' , 'Red' , 'Silver' , 'Grey', 'Other'};
pie(X, Labels)
%I also tried to make the percentages as a vector
Y = [45.333, 25.333, 16, 2.666, 4];
pie(Y)
which produce mismatched percentages
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 20 日
White = 34;
Black = 19;
Red = 12;
Silver = 2;
Grey = 3;
White + Black + Red + Silver + Grey
ans = 70
... Does not total 75. You need an "other" which is 75 minus the total.
LeoAiE
LeoAiE 2021 年 4 月 20 日
編集済み: LeoAiE 2021 年 4 月 20 日
Hi Walter,
Thank you so much for responding I added ‘Other’ = 5 , and edited the code… and sorry about that by the way I was just making up data to get my question across. Back to the question, how to match the pie and the actual precentage ... Or I just got everything wrong / confusing myself :)

サインインしてコメントする。

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 4 月 21 日
編集済み: Scott MacKenzie 2021 年 4 月 21 日
Is the central issue combining a text label with the percentage? If so, perhaps this will work:
X = [34 19 12 2 3 5];
Labels = {'White', 'Black' , 'Red' , 'Silver' , 'Grey', 'Other'};
xPercent = X / sum(X) * 100;
newLabels = [];
for i=1:length(X)
newLabels = [newLabels {sprintf('%s (%.1f%%)', Labels{i}, xPercent(i))}];
end
pie(X, newLabels);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePie Charts についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by