add text above the legend on pie chart

조회 수: 17 (최근 30일)
Alberto Acri
Alberto Acri 2023년 9월 13일
댓글: Les Beckham 2023년 9월 13일
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