change the legend of a pie chart

9 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 9 月 23 日
コメント済み: Voss 2023 年 9 月 24 日
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 Center および File ExchangeLegend についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by