change the legend of a pie chart

6 次查看(过去 30 天)
Hi. I have the following pie chart:
matrix_new = importdata("matrix_new.mat"); % import
labels = matrix_new(:,1);
percentages = matrix_new(:,2);
figure
p = pie(percentages);
hText = p(2:2:end);
set(hText,{'String'},compose('%d',labels));
label_str = compose('%d (%g%%)',matrix_new);
% label_str_new = importdata("label_str_new.mat"); % import
% label_str = label_str_new;
set(hText,'FontSize',12);
pPatch = p(1:2:end);
cm_c = importdata("cm_c.mat"); % import
set(pPatch,{'FaceColor'},num2cell(cm_c,2));
lgd = legend(label_str,'Location','EastOutside','FontSize',12);
lgd.NumColumns = 4;
I need to transform the legend like this:

采纳的回答

Voss
Voss 2023-9-23
matrix_new = importdata("matrix_new.mat"); % import
labels = matrix_new(:,1);
percentages = matrix_new(:,2);
figure
p = pie(percentages);
hText = p(2:2:end);
set(hText,{'String'},compose('%d',labels));
label_str = compose('%d (%g%%)',matrix_new);
% label_str_new = importdata("label_str_new.mat"); % import
% label_str = label_str_new;
set(hText,'FontSize',12);
pPatch = p(1:2:end);
cm_c = importdata("cm_c.mat"); % import
set(pPatch,{'FaceColor'},num2cell(cm_c,2));
[legend_labels,idx] = unique(label_str,'stable');
lgd = legend(pPatch(idx),legend_labels,'Location','EastOutside','FontSize',12);
  2 个评论
Alberto Acri
Alberto Acri 2023-9-24
Thanks for your answer! However, if I want to replace 'label_str' with an imported 'label_str' having the same number of rows as the number of colors in the graph, how can I do it?
label_str_new = importdata("label_str_new.mat"); % import
label_str = label_str_new;
I tried inserting using your lines of code but it doesn't give the same result.
Voss
Voss 2023-9-24
matrix_new = importdata("matrix_new.mat"); % import
labels = matrix_new(:,1);
percentages = matrix_new(:,2);
figure
p = pie(percentages);
hText = p(2:2:end);
set(hText,{'String'},compose('%d',labels));
[~,idx] = unique(compose('%d (%g%%)',matrix_new),'stable');
set(hText,'FontSize',12);
pPatch = p(1:2:end);
cm_c = importdata("cm_c.mat"); % import
set(pPatch,{'FaceColor'},num2cell(cm_c,2));
label_str = load('label_str_new.mat');
label_str = label_str.label_str_new;
lgd = legend(pPatch(idx),label_str,'Location','EastOutside','FontSize',12);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by