How to add labels to a pie chart.

조회 수: 7 (최근 30일)
Sara Thoms
Sara Thoms 2021년 4월 14일
답변: Abhiram 2025년 2월 17일
Hi all, I want to add labels to a chart that is made out of a vector given by the user, and if the sum of the given numbers is less then 100, then the pie will show the percentage of each number, the deduction differnce will be labeld 'other'. I don't know how many numbers the user gives.
example [1,5,6,8]
will sow 1% 5% 6% 8% and the (100-20=80) will be labeld 'other'

답변 (1개)

Abhiram
Abhiram 2025년 2월 17일
To get the deduction difference of the vector elements from 100 and label it in the pie chart as ‘Other’, you can follow the given steps:
  • Append the difference (100 – sum(vector)) to the given vector itself.
  • Append the ‘Other’ label to the labels array.
You can refer to the following code as an example:
x = [10, 20, 40];
other = 100 - sum(x);
labels = ["first", "second", "third"];
if other > 0
x = [x, other];
labels = [labels, "other"];
end
pie(x, labels);
For additional guidance, you can refer to the MATLAB Documentation on the “pie” function:

카테고리

Help CenterFile Exchange에서 Pie Charts에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by