Legend for tiled donut charts

14 views (last 30 days)
I have a 2x3 tiled layout.
In the first row, I put three donut charts (one per each "cell"). The figure is attached.
What I want to do is to put a unique legend for them in the whole second row. I saw that this is possible for other types of charts and interesting options were suggested to other users. The main problem is that the properties for the legend of donut charts are very limited.
Suggestions?
Thank you!

Accepted Answer

Voss
Voss on 28 Jan 2025
Here's something that may help.
n = 3;
data = rand(10,n);
f = figure();
tl = tiledlayout(2,n);
d = gobjects(n,1);
for ii = 1:n
nexttile(tl)
d(ii) = donutchart(data(:,ii));
end
tl = tiledlayout(tl,1,1);
tl.Layout.Tile = n+1;
tl.Layout.TileSpan = [1,n];
ax = nexttile(tl);
ax.Visible = 'off';
c = d(1).ColorOrder;
nc = size(c,1);
nd = numel(d(1).Data);
c = repmat(c,ceil(nd/nc),1);
p = cellfun(@(x)patch(ax,NaN,NaN,x),num2cell(c(1:nd,:),2));
set(p,'FaceAlpha',d(1).FaceAlpha);
d(1).LabelsMode = 'manual';
d(1).Names = "slice "+(1:nd);
legend(p,d(1).Names,'Orientation','horizontal','NumColumns',ceil(nd/2));

More Answers (1)

Andrea Costantino
Andrea Costantino on 29 Jan 2025
It helped a lot! Thank you very much!

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!