필터 지우기
필터 지우기

How to unfade legend placed on an empty (invisible) plot?

조회 수: 5 (최근 30일)
Petar Milin
Petar Milin 2024년 3월 29일
답변: Adam Danz 2024년 3월 29일
I am trying to make a figure with three plots, and use the fourth one to place the legend. I set that last plot as invisible but then the legend is also faded. How can I fix that?
Here is the relevant code:
fig = figure();
subplot(1,4,1)
plot(x1(:,[1:15]),'LineWidth',3);
ylim([-0.3 1.2]);
title('X1');
subplot(1,4,2)
plot(x2(:,[1:15]),'LineWidth',3);
ylim([-0.3 1.2]);
title('X2');
subplot(1,4,3)
plot(x3(:,[1:15]),'LineWidth',3);
ylim([-0.3 1.2]);
title('X3');
subplot(1,4,4)
plot(x3(:,[1:15]),'Visible','off');
legend(labels,'FontSize',12,'Location','best');
axis off;
sgtitle('Lines');
set(fig,'Units','normalized','Position',[0 0 3 1.5]);

채택된 답변

Voss
Voss 2024년 3월 29일
Instead of making the lines in the last axes invisible, make them all-NaN so they don't show up but the legend stil appears normal.
% made up variables:
x1 = rand(10,15);
x2 = rand(10,15);
x3 = rand(10,15);
labels = "label"+(1:15);
% your code with last plot() modification:
fig = figure();
subplot(1,4,1)
plot(x1(:,1:15),'LineWidth',3);
ylim([-0.3 1.2]);
title('X1');
subplot(1,4,2)
plot(x2(:,1:15),'LineWidth',3);
ylim([-0.3 1.2]);
title('X2');
subplot(1,4,3)
plot(x3(:,1:15),'LineWidth',3);
ylim([-0.3 1.2]);
title('X3');
subplot(1,4,4)
plot(NaN(2,15));
legend(labels,'FontSize',12,'Location','best');
axis off;
sgtitle('Lines');
% set(fig,'Units','normalized','Position',[0 0 3 1.5]);
  댓글 수: 4
Petar Milin
Petar Milin 2024년 3월 29일
Thanks! Can you please just unwrap (explain) why (2,15) in
plot(NaN(2,15),'LineWidth',3)
Voss
Voss 2024년 3월 29일
OK. In that line you want to plot 15 lines (that's what you had originally), but I want them to be all NaNs. If I give plot a vector, then plot will give me one line only, so I need to use a matrix with 15 columns because plot plots one line per column of a matrix. The smallest matrix with 15 columns is 2x15, so that's what NaN(2,15) gives you, a 2x15 matrix of NaNs:
NaN(2,15)
ans = 2×15
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2024년 3월 29일
It's much better to use tiledlayout than subplot.
With tiledlayout, the placement of the legend and the global title can be part of the layout.
fig = figure();
tcl = tiledlayout(1,4); % <----- used tiledlayout
nexttile() % <----- call nexttile instead of subplot
plot(x1(:,[1:15]),'LineWidth',3);
ylim([-0.3 1.2]);
title('X1');
nexttile() % <----- call nexttile instead of subplot
plot(x2(:,[1:15]),'LineWidth',3);
ylim([-0.3 1.2]);
title('X2');
nexttile() % <----- call nexttile instead of subplot
plot(x3(:,[1:15]),'LineWidth',3);
ylim([-0.3 1.2]);
title('X3');
leg = legend(labels,'FontSize',12,'Location','best');
leg.Layout.Tile = 4 % <----- assign legend to 4th tile location
title(tcl,'Lines'); % <----- Set the title of the entire layout instead of sgtitle
set(fig,'Units','normalized','Position',[0 0 3 1.5]);

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by