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

6 ビュー (過去 30 日間)
Sim 2023 年 6 月 1 日

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?

サインインしてコメントする。

### 採用された回答

Sim 2023 年 6 月 1 日

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);

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLegend についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by