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

조회 수: 5 (최근 30일)
Sim
Sim 2023년 6월 1일
편집: 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
Sim 2023년 6월 1일
편집: 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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specialized Power Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by