how to avoid getting 0 overlap on the pie chart?
조회 수: 16 (최근 30일)
이전 댓글 표시
Hi,
I am using a code below to plot the pie chart but the zero values are over lapping is there a way I can avoid zero onto the pie chart but legend will appear what is associated with zero.
Code:
H = [0 0 5 95];
H = pie(H)
T = H(strcmpi(get(H,'Type'),'text'));
P = cell2mat(get(T,'Position'));
set(T,{'Position'},num2cell(P*0.6,2))
댓글 수: 0
채택된 답변
Star Strider
2022년 9월 25일
The code was not returning the correct position vectors.
I am not certain what you want to do.
This repositions the ‘5%’ text object —
H = [0 0 5 95];
H = pie(H);
% T = H(strcmpi(get(H,'Type'),'text'));
T = findobj(H,'Type','text')
P = arrayfun(@(x)get(x,'Position'),T, 'Unif',0)
% P = cell2mat(get(T,'Position'));
% set(T{3},num2cell(P*0.6,2))
set(T(3),'Position',P{3}+[0.08 -0.5 0])
NOTE — The the ‘0%’ is created twice.
.
댓글 수: 0
추가 답변 (2개)
Geoff Hayes
2022년 9월 25일
@muhammad choudhry - since T is the array of handles to your text objects, you could just iterate through this array and hide those that correspond to '0%'. For example,
for k=1:length(T)
if strcmp(T(k).String, '0%')
T(k).Visible = 'off';
end
end
would hide those labels (if I understand your question correctly).
댓글 수: 0
dpb
2022년 9월 25일
편집: dpb
2022년 9월 25일
I've never tried a legend with a pie chart; I suppose it also probably works somehow...
Well, the patch handle is two objects per value so you can try something similar to
H=[0 0 5 95];
hP=pie(H);
delete(findobj(hP,'String','0%')) % wipe out the two zero text objects
hLG=legend(hP(2*find(H==0)-1),'ZeroA','ZeroB'); % write a legend to the patches associated with 0
hP(1).FaceColor='r'; % can change the color from original black, too...
Alternatively, you can keep the two text objects and move then and rewrite their string data...
hP=pie(H);
hZ=findobj(hP,'String','0%'); % retrieve the zero object handles
p=vertcat(hZ.Position); % and their position
p=p+[-0.7 0 0;-0.7 -0.1 0]; % arbitrary position adjustment to left and down for second
set(hZ,{'Position'},mat2cell(p,[1 1],3)) % rewrite new position
set(hZ,{'String'},{'ZeroA';'ZeroB'})
The simplest alternative is probably to just not draw the zeros in the first place...
hP=pie(H(find(H)));
hTxt=text(-1,-0.9,{'TextA';'TextB'});
You can write additional text at will outside the use of the pie chart entirely. (HINT: I determined the x,y positions there by first having done
hAx=gca;xlim,ylim
to see what the range of the axes limits is by default with a pie chart to have an idea where to put them out of the way, by on the visible area.)
So, the question not given is what do the zeros correspond to and what's to be written/displayed regarding them?
Various choices; "salt to suit" how want to deal with...
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Pie Charts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!