how to avoid getting 0 overlap on the pie chart?

12 views (last 30 days)
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))

Accepted Answer

Star Strider
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);
H =
1×8 graphics array: Patch Text Patch Text Patch Text Patch Text
% T = H(strcmpi(get(H,'Type'),'text'));
T = findobj(H,'Type','text')
T =
4×1 Text array: Text (0%) Text (0%) Text (5%) Text (95%)
P = arrayfun(@(x)get(x,'Position'),T, 'Unif',0)
P = 4×1 cell array
{[6.7356e-17 1.1000 0]} {[6.7356e-17 1.1000 0]} {[ -0.1721 1.0865 0]} {[ 0.1721 -1.0865 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.
.

More Answers (2)

Geoff Hayes
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).

dpb
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
ans = 1×2
-1.2000 1.2000
ans = 1×2
-1.2000 1.2000
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...

Community Treasure Hunt

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

Start Hunting!