Filter löschen
Filter löschen

change the legend of a pie chart

6 Ansichten (letzte 30 Tage)
Alberto Acri
Alberto Acri am 23 Sep. 2023
Kommentiert: Voss am 24 Sep. 2023
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:

Akzeptierte Antwort

Voss
Voss am 23 Sep. 2023
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 Kommentare
Alberto Acri
Alberto Acri am 24 Sep. 2023
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 am 24 Sep. 2023
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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by