# If we have 2 Legend's outputs, the "Number of Columns" option does not work

3 views (last 30 days)
Sim on 1 Jun 2023
Edited: Sim on 1 Jun 2023
Case 1. If we add the "Number of Columns" option to the Legend, and if we have 2 Legend's outputs, the number of columns does not change:
N = 100;
x = linspace(0,1,N);
y1 = repmat([-2;2],1,N);
y2 = repmat([-1;1],1,N);
figure; hold on;
h1 = fill([x flip(x)], [y1(1,:) flip(y1(2,:))],'r');
h2 = fill([x flip(x)], [y2(1,:) flip(y2(2,:))],'g');
ylim([-2,2]);
[first,second] = legend('area1','area2','NumColumns',2); % <-- 2 Legend's outputs
PatchInLegend = findobj(second, 'type', 'patch'); % <-- I need this part AND 'NumColumns',2
set(PatchInLegend(1), 'FaceAlpha', 0.2); % <-- I need this part AND 'NumColumns',2
set(PatchInLegend(2), 'FaceAlpha', 0.4); % <-- I need this part AND 'NumColumns',2
Case 2. If we add the "Number of Columns" option to the Legend, and if we have 1 Legend's output, the number of columns changes:
N = 100;
x = linspace(0,1,N);
y1 = repmat([-2;2],1,N);
y2 = repmat([-1;1],1,N);
figure; hold on;
h1 = fill([x flip(x)], [y1(1,:) flip(y1(2,:))],'r');
h2 = fill([x flip(x)], [y2(1,:) flip(y2(2,:))],'g');
ylim([-2,2]);
first = legend('area1','area2','NumColumns',2); % <-- 1 Legend's output
Question. How to make the "Number of Columns" option work if we have 2 Legend's outputs?
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Accepted Answer

Sim on 1 Jun 2023
Edited: Sim on 1 Jun 2023
I might have found a solution, but the "FaceAlpha" option acts on both the figure patches and the legend symbols:
N = 100;
x = linspace(0,1,N);
y1 = repmat([-2;2],1,N);
y2 = repmat([-1;1],1,N);
figure; hold on;
h1 = fill([x flip(x)], [y1(1,:) flip(y1(2,:))],'r');
h2 = fill([x flip(x)], [y2(1,:) flip(y2(2,:))],'g');
ylim([-2,2]);
first = legend('area1','area2','NumColumns',2);
PatchInLegend = findobj(first.PlotChildren, 'type', 'patch');
set(PatchInLegend(1), 'FaceAlpha', 0.2);
set(PatchInLegend(2), 'FaceAlpha', 0.4);
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Categories

Find more on Legend in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!