create a pie subgraph with assigned percentage values

1 次查看(过去 30 天)
I am using the following code to create a pie chart:
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d ', number(~idx)).';
nrc{end+1} = [num2str(combine) ' '];
cattxt = cellfun(@(x,y)cat(2,x,y),nrc, percentVals, 'Unif',0);
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch)));
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:);
pText(k).String = [];
end
legend(cattxt, 'Location','eastoutside')
I would like to create a new pie chart named 'example', smaller and next to the main one (pie chart on the left), with the percentage values of '78 79 87 88' representing the numbers with % <=6% (I had set idx = pcts<7;).
I would like to get this pie chart as the end result (the colors, at the moment, are fine random):

采纳的回答

Voss
Voss 2023-9-1
number = [78 79 80 81 82 83 84 85 86 87 88];
value = [4509 5239 6400 9074 11047 13147 15137 13909 6354 1152 183];
str = 'example';
font_size = 9;
pcts = 100*value./sum(value);
idx = pcts < 7;
figure();
tiledlayout(1,3,'TileSpacing','none')
ax = nexttile();
p = pie(ax,[value(~idx) sum(value(idx))]);
t = p(2:2:end);
p = p(1:2:end);
set(t,{'String'},[repmat({''},numel(t)-1,1); {str}],'FontSize',font_size);
set(p,{'FaceColor'},[num2cell(turbo(numel(p)-1),2); {[1 1 1]}]);
ax.XLim = [-1 1];
ax.YLim = [-1 1];
p_all = p(1:end-1);
ax = nexttile();
p = pie(ax,value(idx));
t = p(2:2:end);
p = p(1:2:end);
set(t,'String','');
set(p,{'FaceColor'},num2cell(summer(numel(p)),2));
text(ax,0,1.2,str, ...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom', ...
'FontSize',font_size)
ax.XLim = [-1.5 1.5];
ax.YLim = [-1.5 1.5];
p_all = [p_all p];
[~,idx] = ismember(number,[number(~idx) number(idx)]);
p_all = p_all(idx);
pcts = round(pcts);
txt = compose('%d (%d%%)',[number; pcts].');
idx = pcts == 0;
txt(idx) = compose('%d (<1%%)',number(idx));
ax = nexttile();
ax.Visible = 'off';
ax.YDir = 'reverse';
N = numel(number);
line(ax,[0 2 2 0 0],[0 0 N+1 N+1 0],'Color','k','Clipping','off')
text(ax,0.9*ones(1,N),1:N,txt,'FontSize',font_size)
patch(ax, ...
'XData',zeros(1,N)+0.5+0.3*[-1;1;1;-1;-1], ...
'YData',(1:N)+0.4*[-1;-1;1;1;-1], ...
'FaceColor','flat', ...
'FaceVertexCData',cell2mat(get(p_all,'FaceColor')))
ax.XLim = [0 2];
ax.YLim = [-5 N+6];

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Pie Charts 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by