Azzera filtri
Azzera filtri

pie chart not complete - a pie is missing

12 visualizzazioni (ultimi 30 giorni)
Alberto Acri
Alberto Acri il 20 Set 2023
Modificato: Voss il 20 Set 2023
HI! Why is the pie chart not complete? I would like to take into account the values present in 'matrix_analysis'.
load matrix_analysis.mat
labels = matrix_analysis(:,1);
percentages = matrix_analysis(:,2);
load legend_use.mat
% Create the pie chart with labels
figure
p = pie(percentages);
% Set 'TextType' property to 'none' to remove percentage labels
hText = findobj(p, 'Type', 'text');
percentValues = get(hText, 'String');
combinedText = strcat(percentValues, ' %');
for i = 1:numel(hText)
set(hText(i), 'String', '');
end
str = [string(labels') ""];
for k=1:numel(hText)
hText(k).String = str(k);
end
% Display the matrix_new values in the pie chart
label_str = arrayfun(@(x, y) sprintf('%d (%d%%)', x, y), matrix_analysis(:, 1), matrix_analysis(:, 2), 'UniformOutput', false);
% Dimension number around pie chart
for i=1:numel(labels)
strObj = findobj(p, 'String', labels(i)); % find the label in pie chart
set(strObj, 'FontSize', 14); % increase font size of label
end
Unrecognized function or variable 'dim_numbers'.
pPatch = findobj(p, 'Type','Patch');
% Color
cm = colormap(turbo(numel(pPatch)));
for k = 1:numel(label_str)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
end
% Add labels to the pie chart
lgd = legend(legend_use, 'Location', 'EastOutside','FontSize',14);
Also, is it possible to arrange the numbers along the radius? In this way (example)!

Risposta accettata

Voss
Voss il 20 Set 2023
Modificato: Voss il 20 Set 2023
The pie chart is incomplete because the numbers in your variable "percentages" do not sum to 1. They sum to 0.91, so you get a 9%-size missing slice. Relevant documentation from pie:
pie(X) draws a pie chart using the data in X. Each slice of the pie chart represents an element in X.
  • If sum(X) ≤ 1, then the values in X directly specify the areas of the pie slices. pie draws only a partial pie if sum(X) < 1.
  • If sum(X) > 1, then pie normalizes the values by X/sum(X) to determine the area of each slice of the pie.
See below:
dim_numbers = 12; % a guess
load matrix_analysis.mat
labels = matrix_analysis(:,1);
percentages = matrix_analysis(:,2);
% load legend_use.mat
% not using legend_use.mat; I'll use the legend labels constructed below (label_str)
disp(sum(percentages))
0.9100
See below for how you can adjust the Rotation property of the text object to make them oriented along radii. (I've also commented out parts of your code and replaced them with more efficient and succinct alternatives.)
% Create the pie chart with labels
figure
p = pie(percentages);
% hText = findobj(p, 'Type', 'text');
% no need for findobj(); you have the text objects in the array p already
hText = p(2:2:end);
% percentValues = get(hText, 'String');
% combinedText = strcat(percentValues, ' %');
% for i = 1:numel(hText)
% set(hText(i), 'String', '');
% end
%
% str = [string(labels') ""];
% for k=1:numel(hText)
% hText(k).String = str(k);
% end
% no need to set the Strings and then set them again;
% use compose to construct the label cell array of chars;
% use cell syntax to set a property of an array of objects to different values
% at once (avoid the loop)
set(hText,{'String'},compose('%d',labels));
% Display the matrix_new values in the pie chart
% label_str = arrayfun(@(x, y) sprintf('%d (%d%%)', x, y), matrix_analysis(:, 1), matrix_analysis(:, 2), 'UniformOutput', false);
% compose is more succinct than arrayfun+sprintf
label_str = compose('%d (%g%%)',matrix_analysis);
% Dimension number around pie chart: increase font size of label
% for i=1:numel(labels)
% strObj = findobj(p, 'String', labels(i)); % find the label in pie chart
% set(strObj, 'FontSize', dim_numbers); % increase font size of label
% end
% no need for findobj(); you have the text objects in the array p already.
% no need for the for loop; just set all the texts' fontsizes at once.
set(hText,'FontSize',dim_numbers);
% adjust the rotation angles of the texts:
pos = cell2mat(get(hText,'Position'));
angles = atan2d(pos(:,2),pos(:,1));
idx = pos(:,1) < 0;
angles(idx) = angles(idx)-180;
set(hText,{'Rotation'},num2cell(angles));
% left-half plane: align right; right-half plane: align left
set(hText(~idx),'HorizontalAlignment','left');
set(hText(idx),'HorizontalAlignment','right');
% pPatch = findobj(p, 'Type','Patch');
% no need for findobj(); you have the objects already in the array p
pPatch = p(1:2:end);
% Color
% cm = colormap(turbo(numel(pPatch)));
% no need to set the figure's colormap (it's unused), which is what colormap() does;
% just use the colors you get from the turbo() function
cm = turbo(numel(pPatch));
% for k = 1:numel(label_str)
% pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
% end
% use cell syntax to set a property of an array of objects to different
% values at once:
set(pPatch,{'FaceColor'},num2cell(cm,2));
% Add labels to the pie chart
% lgd = legend(legend_use,'Location','EastOutside','FontSize',dim_numbers);
% use the label_str just constructed above
lgd = legend(label_str,'Location','EastOutside','FontSize',dim_numbers);
  1 Commento
Voss
Voss il 20 Set 2023
Modificato: Voss il 20 Set 2023
For reference, the complete code again, with explanatory comments and old code removed (and tightened up a little more):
dim_numbers = 12; % a guess
load matrix_analysis.mat
labels = matrix_analysis(:,1);
percentages = matrix_analysis(:,2);
% Create the pie chart with labels
figure
p = pie(percentages);
hText = p(2:2:end);
% update text sting and fontsize:
set(hText,{'String'},compose('%d',labels),'FontSize',dim_numbers);
% adjust the rotation angles of the texts:
pos = cell2mat(get(hText,'Position'));
angles = atan2d(pos(:,2),pos(:,1));
idx = pos(:,1) < 0;
angles(idx) = angles(idx)-180;
set(hText,{'Rotation'},num2cell(angles));
% left-half plane: align right; right-half plane: align left
set(hText(~idx),'HorizontalAlignment','left');
set(hText(idx),'HorizontalAlignment','right');
% Color
set(p(1:2:end),{'FaceColor'},num2cell(turbo(numel(p)/2),2));
% Add labels to the pie chart
lgd = legend(compose('%d (%g%%)',matrix_analysis), ...
'Location','EastOutside','FontSize',dim_numbers);

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by