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

3 ビュー (過去 30 日間)
Mary Ng
Mary Ng 2015 年 12 月 5 日
回答済み: emehmetcik 2015 年 12 月 9 日
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 Center および File ExchangePie Charts についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by