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

2 次查看(过去 30 天)
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!

回答(2 个)

emehmetcik
emehmetcik 2015-12-6
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 个评论
Mary Ng
Mary Ng 2015-12-6
I can't change the values. I know how to. But they're fixed values that cannot be changed. Is there any other method?

请先登录,再进行评论。


emehmetcik
emehmetcik 2015-12-9
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')

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by