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

9 次查看(过去 30 天)
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 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by