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

6 次查看(过去 30 天)
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