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

4 views (last 30 days)
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]);

Accepted Answer

Voss
Voss on 29 Mar 2024
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 Comments
Petar Milin
Petar Milin on 29 Mar 2024
Thanks! Can you please just unwrap (explain) why (2,15) in
plot(NaN(2,15),'LineWidth',3)
Voss
Voss on 29 Mar 2024
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>

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 29 Mar 2024
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]);

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!