![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288312/image.png)
Pie chart label overlapping
조회 수: 70 (최근 30일)
이전 댓글 표시
Hey guys so I have a pie chart with labels like 1%,2% <1% and because there are 100 values it overlaps quite a lot.
I am able to remove all labels with delete(findobj(p,'Type','Text'))
Is there any way to remove just the ones with <1% or somehow group the labels together?
댓글 수: 0
채택된 답변
Adam Danz
2020년 4월 28일
편집: Adam Danz
2024년 10월 4일
> Is there any way to remove just the ones with <1% or somehow group the labels together?
Starting in MATLAB R2024b, piechart offers the NumDisplayWedges property that lets you set the number of wedges and puts the remaining date into an "other" category. When used with the DisplayOrder property, you can show all data that is greater than 1%.
data = [linspace(0,1,20),linspace(.5,2,10),linspace(1,10,20)];
h = piechart(data,'DisplayOrder','descend','LabelStyle','percent');
h.NumDisplayWedges = sum(h.Data>1)-1; % show all data >1%
Using the older pie function
Use the pie() output handle to obtain the text objects.
h = pie(. . .);
th = findobj(h,'Type','Text'); % text handles
Determine which text strings begin with "<"
isSmall = startsWith({th.String}, '<'); % r2016b or later
% isSmall = ~cellfun(@isempty, regexp({th.String},'^<')); % any matlab release
Either delete the text objects or replace their String values with empties.
delete(th(isSmall));
%or
set(th(isSmall),'String', '')
Demo:
% h = pie(sort(data));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288312/image.png)
댓글 수: 1
Giuseppe Degan Di Dieco
2021년 5월 24일
Dear Adam,
you're awesome, your help was so precious.
Thanks, best.
추가 답변 (1개)
Toder
2020년 4월 28일
편집: Toder
2020년 4월 28일
Following the documentation for pie chart labels at https://www.mathworks.com/help/matlab/creating_plots/customize-pie-chart-labels.html, we can get the labels. Then a simple for loop can remove the ones you don't want.
x = [0.1 50 50];
p = pie(x);
pText = findobj(p,'Type','text');
for i=1:length(x)
if strcmp(pText(i).String,'< 1%')
pText(i).String = '';
end
end
댓글 수: 1
Giuseppe Degan Di Dieco
2021년 5월 24일
Hi Toder,
your tip is brilliant too.
Thanks for your help, and best!
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!