How do I add label/Legend, percentage, and explode all at once?

5 views (last 30 days)
What I have so far is,
(See Attached Photo, left pie)
x = [0 46 0 57 90];
explode = [0 0 0 0 1];
labels = {'A', 'B', 'C', 'D', 'E'};
title('Title here!')
pie(x, labels)
Which gives me only the correct label(Since 1&3 are zeros, it does not show up. Only 2,4,5 AND it labels correctly), but with no percentage, or explode
BUT, if I were to do it this way.
(See Attached Photo, right pie)
explode = [0 0 0 0 1];
labels = {'A', 'B', 'C', 'D', 'E'};
pie(pieChart,explode)
title('Title here')
legend(labels, 'Location', 'southoutside', 'Orientation', 'horizontial');
This way gives the percentage, explode, but the incorrect legend label. Because 1,3 is zeros. It still shows as A,B,C in the legend. Ideally I can just do labels = {'B', 'D', 'E'}; but I don't want to do it manually. Because I'm certain there is a way
I don't care if I have legend or labels, as long as it has the correct label.
Sorry if I'm not clear, and thank you!

Answers (2)

emehmetcik
emehmetcik on 6 Dec 2015
You can try using very small percentages instead of zeros:
x = [0 46 0 57 90]+eps; % Make them all non-zero
explode = [0 0 0 0 1];
labels = {'A', 'B', 'C', 'D', 'E'};
pie(x,explode)
title('Title here')
legend(labels, 'Location', 'southoutside', 'Orientation', 'horizontial');
  1 Comment
Mary Ng
Mary Ng on 6 Dec 2015
I can't change the values. I know how to. But they're fixed values that cannot be changed. Is there any other method?

Sign in to comment.


emehmetcik
emehmetcik on 9 Dec 2015
Not an elegant way but try this:
x = [0 46 0 57 90];
x_idx = 1 : numel(x);
explode = ones(size(x));
xx = x(x~=0);
x_idx = x_idx(x~=0);
labels{1} = 'A';
labels{2} = 'B';
labels{3} = 'C';
labels{4} = 'D';
labels{5} = 'E';
h = pie(x, explode);
for k = 1 : numel(xx)
set(h(2*k-1), 'DisplayName', labels{x_idx(k)})
end
title('Title here')
legend('toggle')

Tags

Community Treasure Hunt

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

Start Hunting!