Azzera filtri
Azzera filtri

Arrange numbers circularly around the pie chart

3 visualizzazioni (ultimi 30 giorni)
I create this graph chart:
matrix = importdata("matrix_out_INF.mat");
% ===============
% Filter the matrix to include only rows with percentage values < 5
matrix_new = matrix(matrix(:,2) < 5, :);
tot_percent_subgraph = sum(matrix_new(:,2));
% Extract the labels and corresponding percentages for the pie chart
labels = matrix_new(:,1);
percentages = matrix_new(:,2);
% 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_new(:, 1), matrix_new(:, 2), 'UniformOutput', false);
% Colore
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
for k = 1:numel(label_str)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
% pText(k).FontSize = 12;
end
% Add labels to the pie chart
lgd = legend(label_str, 'Location', 'EastOutside','FontSize',12);
lgd.NumColumns = 2;
I would like:
1) Display around the pie chart only the values of 'labels1':
[r,c] = find(percentages == 0);
labels1 = labels;
labels1(r) = [];
2) Try to have a circular arrangement of numbers (see figure), without the numbers overlapping each other.
3) Possiblity to change the size of the numbers around the pie chart. I have tried this modification but it does not lead to any change:
[r,c] = find(percentages == 0);
% labels1 = labels;
% labels1(r) = [];
label_str_agg = label_str;
label_str_agg(r) = [];
for k1 = 1:numel(label_str_agg)
pText(k1).FontSize = 20;
end

Risposta accettata

akshatsood
akshatsood il 8 Set 2023
Modificato: akshatsood il 8 Set 2023
Hi Alberto,
I understand that you are encountering issues while plotting data on the pie chart. From my understanding of the question, your requirements are as follows
  • You want to hide the labels for strings with percentage = 0
  • Achieve circular arrangement of numbers without overlapping
I ran your code on my end to reproduce the issue you are facing. I observed that the overlapping is because you are plotting data points with 0% share on the pie chart. In case you do not wish to show the labels with 0% share on the legend and resolve the overlapping the issue, you can modify the following line in your code
matrix_new = matrix(matrix(:,2) < 5, :);
to the following line. This will omit the labels having percentage == 0
matrix_new = matrix((matrix(:,2) < 5) & (matrix(:,2) > 0), :);
The pie chart obtained on executing the code after modification is shown below
To accommodate both of your requirements, you can extract all the labels with percentage == 0. Once you have the list, leverage the findobj function to get handles to each label and then use set to modify the 'String' property to ''
x = labels(percentages == 0); % extract all labels with percentage == 0
for i=1:numel(x)
textObj = findobj(p, 'String', x(i)); % find the label in pie chart
set(textObj, 'String', ''); % hide the label
end
I hope this helps.
  5 Commenti
Alberto Acri
Alberto Acri il 12 Set 2023
Hi @akshatsood ! I wanted to ask you another question. In the last graph you showed me I asked you to exclude the numbers around the pie chart that had the value of 0% (so the numbers 54,55,56,57,58,59,88 using these lines of code:
x = labels(percentages == 0); % extract all labels with percentage == 0
for i=1:numel(x)
textObj = findobj(p, 'String', x(i)); % find the label in pie chart
set(textObj, 'String', ''); % hide the label
end
How can I (keeping the legend unchanged) exclude the numbers around the pie chart that presented the value of 0% but only the number 88 (last in the matrix 'matrix_new').
I should edit this line but I don't know how:
x = labels(percentages == 0);
Thanks for your help!
akshatsood
akshatsood il 13 Set 2023
If I understand correctly, you are specifically asking for excluding all except the last element while plotting the pie chart. Based on your requirements, you can modify the specific line of code in the following way
for i=1:numel(x)-1 % leave out the last element in x
In this way, you can exclude the label '88' and prevent any operations on it.

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