Legend behind Axes and Axes Objects

20 views (last 30 days)
curoi
curoi on 20 Nov 2014
Commented: Cameron Brown on 25 Feb 2016
I have a figure with two subplots, both with a legend each, however, the second legend looks like it's stuck behind the second subplot axes:
xticker = linspace( h.(hstruc_nm{ i }).time( 1, 1 ), ...
h.(hstruc_nm{ i }).time( end, 1 ), 10 );
Fig1 = figure( 'Color', 'w', 'PaperPositionMode', 'auto', ...
'Units', 'inches', 'Position', [ 0.01 0.01 9.49 6.24 ] );
ax1 = subplot( 2, 1, 1 );
cla;
hold off;
plot( h.(hstruc_nm{ i }).time, ...
h.(hstruc_nm{ i }).height - mean( h.(hstruc_nm{ i }).height ), ...
'or', 'LineWidth', 2 );
hold on;
plot( h.(hstruc_nm{ i }).dts, h.(hstruc_nm{ i }).dhgt, ...
'om', 'LineWidth', 2 );
plot( h.(hstruc_nm{ i }).dts( 1:length( h.(hstruc_nm{ i }).xout2 ), 1 ),....
h.(hstruc_nm{ i }).xout2, ...
'og', 'LineWidth', 2 );
legend( ax1, 'Demeaned observed', 'High-pass pk. filter', ...
'HA Rayleigh = 0.9', 'Location', 'Northeast' )
datetick( 'x', 'mm/dd', 'keepticks' );
xlabel( 'Date in 1877')
ylabel( 'Detrended water level, m' );
set(gca,'XTick', xticker);
title( 'Original, Filtered, and HA TSR' );
ax2 = subplot( 2, 1, 2 );
cla;
hold off;
plot( h.(hstruc_nm{ i }).dts( 1:length( h.(hstruc_nm{ i }).xout ), 1),...
h.(hstruc_nm{ i }).xout, ...
'or', 'LineWidth', 2 );
hold on;
plot( h.(hstruc_nm{ i }).dts( 1:length( h.(hstruc_nm{ i }).xout2 ), 1 ),....
h.(hstruc_nm{ i }).xout2, ...
'og', 'LineWidth', 2 );
plot( h.(hstruc_nm{ i }).dts( 1:length( h.(hstruc_nm{ i }).xout3 ), 1 ),...
h.(hstruc_nm{ i }).xout3, ...
'ob', 'LineWidth', 2 );
plot( h.(hstruc_nm{ i }).pktime, h.(hstruc_nm{ i }).pkhgt, ...
'oc', 'LineWidth', 2 );
plot( tpred( indpkt, 1 ), h.(hstruc_nm{ i }).ypred2( indpkt, 1 ), ...
'-r', 'LineWidth', 2 );
legend( ax2, 'HA Rayleigh = 1.0', 'HA Rayleigh = 0.9', ...
'HA Rayleigh = 0.8', 'HiLo', 'HA Rayleigh = 0.9 Predicted', ...
'Location', 'Northeast' );
datetick( 'x', 'mm/dd', 'keepticks' );
xlabel( 'Date in 1877');
ylabel( 'Detrended water level, m' );
title( 'R_t_tide HA TSR' );
set( ax2, 'XTick', xticker, 'Layer', 'bottom', 'Color', 'none' );
suptitle( cellstr( h.(hstruc_nm{ i }).name ) );
hgexport( Fig1, strcat( savepath, ...
['\Output\1877_', h.(hstruc_nm{ i }).name, '_harmonic_analysis.png'] ), ...
myStyle2, 'Format', 'png' );
hgexport( Fig1, strcat( stashdir, ['\1877_harmonic_analysis_plots\1877_', ...
h.(hstruc_nm{ i }).name, '_harmonic_analysis.png'] ), ...
myStyle2, 'Format', 'png' );
I've tried setting the subplot axes 'Layer' property to 'bottom', and also the property 'Color' to 'none' but it still sets the second legend behind all the axes graphics objects in the second plot. Everything works fine in the first plot though. Why would the legend get put behind the axes and axes objects?
  2 Comments
Cameron Brown
Cameron Brown on 25 Feb 2016
I have a similar problem. I have several legends which I would like to be stacked on top of the AXES objects in a figure window. I used to be able to use UISTACK to achieve this, but after upgrading to R2015b it no longer works.
Here is an example:
% make some data for plotting
x=randn(100,1);
y=2*x + randn(100,1);
z=x.^2 + randn(100,1);
% generate plots and legends
hFig=figure;
subplot(2,1,1)
plot(x,y,'bx','DisplayName','Linear')
hL1=legend('show');
subplot(2,1,2)
plot(x,z,'rx','DisplayName','Quadratic')
hL2=legend('show');
% move legends over the opposite plot to visualize stacking
hL1.Position=[0.79 0.32 0.2 0.2];
hL2.Position=[0.79 0.57 0.2 0.2];
% try to reorder stack to put legends on top
uistack([hL1 hL2],'top')
% display object stack order (legends have not been moved to top of stack)
disp(hFig.Children)
I want the stack to be: Legend, Legend, Axes, Axes However, after using UISTACK, the stack order is still: Legend, Axes, Legend, Axes
Is there a workaround to get the desired stack order?

Sign in to comment.

Answers (1)

matt dash
matt dash on 22 Nov 2014
The "layer" property controls the layering of axes compared to objects within the axes. Your problem is different: the layering of axes compared to each other. This is controlled by the order they appear in the list get(Fig1,'children'). You can either reorder that list manually, or use the uistack function to reorder it. Something like L = legend(...) ; uistack(L,'top')
Put the uistack at the end of your code so you can be sure it doesnt get undone by some later line.
As for why it's happening: i have no idea, but I recently ran into the exact same problem. I solved it with the more extreme measure of making my own legend class based on an axes. A little more work, but absolutely no surprises.

Community Treasure Hunt

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

Start Hunting!