add text above the legend on pie chart
9 次查看(过去 30 天)
显示 更早的评论
Hi! I would like to add text above the legend (as shown). Is there the possibility of doing this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1481051/image.png)
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);
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!