필터 지우기
필터 지우기

Apply a color to a slice of the pie chart

조회 수: 7 (최근 30일)
Alberto Acri
Alberto Acri 2023년 9월 3일
댓글: Dyuman Joshi 2023년 9월 8일
I need to apply white color to a slice of the pie chart. I used the following code but it creates the slice in black color even though I applied white color [1 1 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]';
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)));
cm1 = [1 1 1]/255; % white color to a pie slice
cm(end,:) = cm1;
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','FontSize',12)

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 9월 3일
"I applied white color [1 1 1]."
Your code doesn't match what you say. You have divided the values by 255, which is doesn't correspond to the white color.
(I have not taken a look at the rest of your code)
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)));
%Corrected value
cm1 = [1 1 1]; % white color to a pie slice
cm(end,:) = cm1;
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','FontSize',12)
  댓글 수: 4
Alberto Acri
Alberto Acri 2023년 9월 8일
Thank you for your reply. Can you tell me how to set the size of the numbers placed around the pie chart (the ones you created)?
Dyuman Joshi
Dyuman Joshi 2023년 9월 8일
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)));
cm1 = [1 1 1];
cm(end,:) = cm1;
pText = findobj(p,'Type','text');
str = [string(number(~idx)) ""];
for k=1:numel(pText)
pText(k).String = str(k);
%%Adjusted font size
pText(k).FontSize = 12;
end
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:);
end
legend(cattxt, 'Location','eastoutside','FontSize',12)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

MYBLOG
MYBLOG 2023년 9월 3일
Your code is almost correct. Do it like this delete /255 :
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)));
cm1 = [1 1 1]; % white color to a pie slice
cm(end,:) = cm1;
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','FontSize',12)

카테고리

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