How to skip legend names if their values equal zero on particular piechat?

4 次查看(过去 30 天)
I am using the following code to plot piechart
labels = {Costs.name};
pie([Costs.GESAMT_ohne_D_Money_min]);
legend(labels);
Sometimes Costs.GESAMT_ohne_D_Money_min could have zero values so they are not printed on pie chart. Because of that labeling becomes wrong as it is not skipping the non-positive values.
How to fix that?

采纳的回答

Image Analyst
Image Analyst 2019-10-8
Try this:
zeroIndexes = [Costs.GESAMT_ohne_D_Money_min] <= 0
labels2 = labels(zeroIndexes); % Extract only the non-zero numbers.
pieData = [Costs.GESAMT_ohne_D_Money_min]; % Initialize
pieData = pieData(zeroIndexes); % Extract only the non-zero numbers.
pie(pieData);
labels(labels2);
  1 个评论
Konstantin Tkachuk
Konstantin Tkachuk 2019-10-14
Thank you for the idea.
This is the working code I have in the result
nonZeroIndexes = [Costs.GESAMT_ohne_D_Money_min] > 0;
labels = {Costs.name};
labels = labels(nonZeroIndexes); % Extract only the positive numbers.
pieData = [Costs.GESAMT_ohne_D_Money_min]; % Initialize
pieData = pieData(nonZeroIndexes); % Extract only the positive numbers.
pie(pieData);
legend(labels);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by