how do i add line to a pie graph

5 views (last 30 days)
Ayan
Ayan on 11 Aug 2023
Commented: dpb on 12 Aug 2023
some of the lable in my pie graphs are over lapping how do I fix this ???? I would like to add lines to the graph so that thehow i would like the graph to look in the end labels are easier to see....
subplot(2,2,1)
normal_behavior = [21 56 24];
p = pie(normal_behavior);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
txt = {'Avoidance of Objects ';'Middle of sidewalk ';'Stops Often '};
combinedtxt = strcat(txt,percentValues);
pText(1).String = combinedtxt(1);
pText(2).String = combinedtxt(2);
pText(3).String = combinedtxt(3);
title("Normal Robot Behvior")
subplot(2,2,2)
unusual_behavior = [2 4 4 33 22 35];
p = pie(unusual_behavior);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
txt = {'Spoke';'Directional changes ';'Got Stuck '; 'Blocked the Sidewalk ';'Unexpected Awareness ';'No Unusual Behavior '};
autopct='%1.1f%%';
pctdistance= 0.5;
labeldistance=1.1;
combinedtxt = strcat(txt,percentValues);
pText(4).String = combinedtxt(4);
pText(5).String = combinedtxt(5);
pText(6).String = combinedtxt(6);
line([.5,5,-3],[.5,2,15])
pText(3).String = combinedtxt(3);
title("Unusual Robot Behavior")
subplot(2,2,3)
unusual_reason = [83 3 13];
p = pie(unusual_reason);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
txt = {'Movement ';'Unexpected location ';'Speech '};
combinedtxt = strcat(txt,percentValues);
pText(1).String = combinedtxt(1);
pText(2).String = combinedtxt(2);
pText(3).String = combinedtxt(3);
title("Reasoning for Unusual Robot Behavior")
subplot(2,2,4)
Delay = [82 18];
p = pie(Delay);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
txt = {'Yes ';'No '};
autopct='%1.1f%%';
pctdistance=0.5;
labeldistance=1.1;
combinedtxt = strcat(txt,percentValues);
pText(1).String = combinedtxt(1);
pText(2).String = combinedtxt(2);
title("Order Delays")

Answers (1)

dpb
dpb on 11 Aug 2023
Edited: dpb on 12 Aug 2023
subplot(2,2,1)
txt=string({['Avoids' newline 'Objects'];['Middle of' newline 'sidewalk'];['Stops' newline 'Often']});
normal_behavior = [21 56 24];
labels=compose('%s %d%%',txt,normal_behavior(:));
p=pie(normal_behavior,labels);
hTxt=findobj(p,'Type','Text');
set(hTxt,{'FontSize'},{7})
Can help a little, but it's a lot of work and MATLAB has no tools to help prettify strings inside graphics text objects. text has no concept of wrapping nor adjusting position by a builtin collision avoidance mechanism; as shown you've got to physically insert a \n to get a second line and avoidance is limited to whatever extents the author of the particular function was able to do in their internal code checking for the function. Also, as observed in your figure, it doesn't know/care about axes boundaries; the 'HorizontalAlignment','right' on the RH pie charts of the annotation on the left of the pie runs over the LH axes while those at the figure boundaries are just clipped. All in all, pretty-much braindead and fixing in a specific case is exceedingly tedious and in general almost hopeless with the present tools without a tremendous amount of effort.
pie is also embarrasingly weak; fixing up the labels inside MATLAB is probably not worth the effort; this is a case where it's likely that exporting the data to Excel or another graphics program is the better route.
Way back when, having MATLAB graphics combined with all the computational power was a godsend; then there simply wasn't anything at all like it as a complete package. Unfortunately, HG2 (handle graphics V2) is now sadly outdated in both ease of use and appearance, particularly evident for such business-like graphics as bars and pies.
About the only way to get the lines will be to try to figure out how to draw them where want/need with annotation and it is a bugger because it (annotation) uses the global coordinate system instead of the axes and with the pie chart you don't really have locations of the axes to use, anyway. You can get the position of the text objects, but then have to compute from the 4-vector position the end and try to guess/compute where the outline of the pie itself is that you're trying to point to; that information isn't available as any sort of object handle.
Wish I had better news/solution, but this is where MATLAB falls short, unfortunately.
  1 Comment
dpb
dpb on 12 Aug 2023
ADDENDUM: And, just to be clear, I'll be "pleased as Punch!" if somebody can come along and show me wrong or just woefully ignorant in regards to these manipulations/modifications.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!