how to avoid getting 0 overlap on the pie chart?
15 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 25 Sep 2022
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 Comments
More Answers (2)
Geoff Hayes
on 25 Sep 2022
@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 Comments
dpb
on 25 Sep 2022
Edited: dpb
on 25 Sep 2022
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 Comments
See Also
Categories
Find more on Pie Charts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!