create a pie chart knowing the percentage value

I have the matrix 'matrix' characterized by the first column by numbers and the second column by percentage values.
matrix = [78,5 ; 79,6 ; 80,7 ; 81,11 ; 82,13 ; 83,15 ; 84,18 ; 85,16 ; 86,7 ; 87,1 ; 88,1];
I would like to create a pie chart that only takes into account percentage values <7.
So the matrix to be considered to create the pie chart would be 'matrix_new':
matrix_new = [78,5 ; 79,6 ; 87,1 ; 88,1];
How can I create the pie chart taking into account percentage values?
I should get something like this:

 Accepted Answer

You can create a pie chart in MATLAB using the pie function, and to consider only the percentage values less than 7, you can filter your matrix accordingly. Here's the MATLAB code to achieve this:
% Your original matrix
matrix = [78,5 ; 79,6 ; 80,7 ; 81,11 ; 82,13 ; 83,15 ; 84,18 ; 85,16 ; 86,7 ; 87,1 ; 88,1];
% Filter the matrix to include only rows with percentage values < 7
matrix_new = matrix(matrix(:, 2) < 7, :);
% Extract the labels and corresponding percentages for the pie chart
labels = matrix_new(:, 1);
percentages = matrix_new(:, 2);
% Create the pie chart with labels
pie(percentages, labels);
% Display the matrix_new values in the pie chart
label_str = arrayfun(@(x, y) sprintf('%d (%d%%)', x, y), matrix_new(:, 1), matrix_new(:, 2), 'UniformOutput', false);
% Title for the pie chart
title('Pie Chart for Percentage Values < 7');
% Add labels to the pie chart
legend(label_str, 'Location', 'EastOutside');
For more information on enhancing your pie charts in MATLAB, you can visit this informative article.

9 Comments

Thanks for your answer. May I know how to delete % number on pie chart? (no inside the legend)
Ok you can do it with this code
% Your original matrix
matrix = [78, 5; 79, 6; 80, 7; 81, 11; 82, 13; 83, 15; 84, 18; 85, 16; 86, 7; 87, 1; 88, 1];
% Filter the matrix to include only rows with percentage values < 7
matrix_new = matrix(matrix(:, 2) < 7, :);
% Extract labels and corresponding percentages for the pie chart
labels = matrix_new(:, 1);
percentages = matrix_new(:, 2);
% Create the pie chart with labels
h = pie(percentages, labels);
% Set 'TextType' property to 'none' to remove percentage labels
hText = findobj(h, 'Type', 'text');
percentValues = get(hText, 'String');
combinedText = strcat(percentValues, ' %');
for i = 1:numel(hText)
set(hText(i), 'String', '');
end
title('Pie Chart for Percentage Values < 7');
% Display the matrix_new values in the pie chart
label_str = arrayfun(@(x, y) sprintf('%d (%d%%)', x, y), matrix_new(:, 1), matrix_new(:, 2), 'UniformOutput', false);
% Title for the pie chart
title('Pie Chart for Percentage Values < 7');
% Add labels to the pie chart
legend(label_str, 'Location', 'EastOutside');
That's fine, but I more specifically delete the number and percent % around the bar graph.
% Your original matrix
matrix = [78, 5; 79, 6; 80, 7; 81, 11; 82, 13; 83, 15; 84, 18; 85, 16; 86, 7; 87, 1; 88, 1];
% Filter the matrix to include only rows with percentage values < 7
matrix_new = matrix(matrix(:, 2) < 7, :);
% Extract labels and corresponding percentages for the pie chart
labels = matrix_new(:, 1);
percentages = matrix_new(:, 2);
% Create the pie chart with labels
h = pie(percentages, labels);
% Set 'TextType' property to 'none' to remove percentage labels
hText = findobj(h, 'Type', 'text');
percentValues = get(hText, 'String');
combinedText = strcat(percentValues, ' %');
for i = 1:numel(hText)
set(hText(i), 'String', '');
end
title('Pie Chart for Percentage Values < 7');
% Display the matrix_new values in the pie chart
label_str = arrayfun(@(x, y) sprintf('%d', x), matrix_new(:, 1), matrix_new(:, 2), 'UniformOutput', false);
% Title for the pie chart
title('Pie Chart for Percentage Values < 7');
% Add labels to the pie chart
legend(label_str, 'Location', 'EastOutside');
May I ask how to split the legend into two columns?
See the legend documentation section on NumColumns.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!