add text above the legend on pie chart

17 views (last 30 days)
Alberto Acri
Alberto Acri on 13 Sep 2023
Commented: Les Beckham on 13 Sep 2023
Hi! I would like to add text above the legend (as shown). Is there the possibility of doing this?
Here is the code:
matrix = importdata("matrix_out_INF_web.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
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
% 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;
text = "percentage values";
lgd1 = legend(text, 'Location', 'northoutside','FontSize',12);

Answers (1)

Adam
Adam on 13 Sep 2023
Edited: Adam on 13 Sep 2023
I can't run your code to test, but
lgd1.Title.String = "percentage values"
should work (instead of passing it into the call to the legend function)
Beware of naming a variable 'text' though - it hides the function of the same name within Matlab.
  3 Comments
Alberto Acri
Alberto Acri on 13 Sep 2023
Thanks for the clarification @Star Strider but the result is not what I wanted to achieve.
I also need to keep the legend on the right. See the image I posted at the beginning of the question.
Les Beckham
Les Beckham on 13 Sep 2023
Just remove the second call to legend, which, I assume, was your attempt at adding the legend title.
See below:
matrix = importdata("matrix_out_INF_web.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
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
% 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;
text = "percentage values";
% lgd1 = legend(text, 'Location', 'northoutside','FontSize',12);
lgd.Title.String = "percentage values"; % <<< add the title here
% text = "percentage values"; % <<< get rid of this
% lgd1 = legend(text, 'Location', 'northoutside','FontSize',12); % <<< get rid of this

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!