Why does legend goes to the back of the axes box in multiplots if you click it or move it with the mouse?

2 views (last 30 days)
juan sanchez
juan sanchez on 27 Nov 2021
Commented: juan sanchez on 29 Nov 2021
Why does legend goes to the back of the axes box in multiplots if you click it or move it with the mouse? This happens specially in subplots or tiled plots (multiplots). I try to move the legend with the mouse somewhere else and immediate is behind the axes box completely concealed and I can not make it visible any more. Only if is on the side of the axes boxes within the gray zone but not inside the axes box where is desired to be.
  3 Comments
juan sanchez
juan sanchez on 29 Nov 2021
Thank you, I am attaching this example and image to explain what is happenig. First, if I try to move the legend with the mouse, the first tile gets all distorted and the legend is concealed behind the axes box and there is no way I can bring it back on top of the axes box. I am using Matlab R2021a. Thank you
[nraw,sraw,navg,savg] = Spectra(V.*0.3048,[],Samp_Freq,'dim',10);
loglog(nraw,sraw,'-.g','LineWidth',1);
grid on
ax = gca;
ax.FontSize = 17;
xlabel('${\rm{n }}\,(Hz)$', 'Interpreter', 'Latex');
ylabel('${S_n}$', 'Interpreter', 'Latex');
leg=legend('Spectra of velocity', '', '', '');
leg.Interpreter='latex';
leg.FontSize = 7;
title(['Probe ',num2str(i_probe)])

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 29 Nov 2021
We can't reproduce the problem without having a minimal working example (e.g. something you provide us that we can run on our end). The code you provided is a decent start but we also need the functions and variable values you're using.
However, I think this may be a problem with how you're creating legends. In your examples, the legends are created without using handles so the legend is assigned to current object which may not correspond to the intended axes.
Instead, use the DisplayName property to assign legend strings to objects and specify axis handles in the legend function.
% Assuming loglog produces 1 handle output,
loglog(nraw,sraw,'-.g','LineWidth',1, 'DisplayName', 'Spectra of Velocity');
grid on
ax = gca;
ax.FontSize = 17;
xlabel('${\rm{n }}\,(Hz)$', 'Interpreter', 'Latex');
ylabel('${S_n}$', 'Interpreter', 'Latex');
leg=legend(ax); % specify axis handle
leg.Interpreter='latex';
leg.FontSize = 7;
title(ax, ['Probe ',num2str(i_probe)]) % use axis handle
% If loglog outputs a vector of 4 handles,
h = loglog(nraw,sraw,'-.g','LineWidth',1); % store output handle
grid on
ax = gca;
ax.FontSize = 17;
xlabel('${\rm{n }}\,(Hz)$', 'Interpreter', 'Latex');
ylabel('${S_n}$', 'Interpreter', 'Latex');
leg=legend(h, 'Spectra of Velocity', '' '' ''); % specify axis handle and strings
leg.Interpreter='latex';
leg.FontSize = 7;
title(ax, ['Probe ',num2str(i_probe)]) % use axis handle

More Answers (0)

Community Treasure Hunt

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

Start Hunting!