add text above the legend on pie chart

18 次查看(过去 30 天)
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);

回答(1 个)

Adam
Adam 2023-9-13
编辑:Adam 2023-9-13
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 个评论
Alberto Acri
Alberto Acri 2023-9-13
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 2023-9-13
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by