MATLAB Answers

0

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

Asked by Konstantin Tkachuk on 8 Oct 2019
Latest activity Commented on by Konstantin Tkachuk on 14 Oct 2019 at 11:09
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?

  0 Comments

Sign in to comment.

Products


Release

R2017b

1 Answer

Answer by Image Analyst
on 8 Oct 2019
 Accepted Answer

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 Comment

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);

Sign in to comment.