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

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

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

카테고리

도움말 센터File Exchange에서 Pie Charts에 대해 자세히 알아보기

태그

질문:

2015년 12월 5일

답변:

2015년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by