change the legend of a pie chart
9 ビュー (過去 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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1491027/image.png)
I need to transform the legend like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1491032/image.png)
0 件のコメント
採用された回答
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 件のコメント
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 Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!