Legend behind Axes and Axes Objects

조회 수: 15 (최근 30일)
curoi
curoi 2014년 11월 20일
댓글: Cameron Brown 2016년 2월 25일
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
Doug Hull
Doug Hull 2014년 11월 21일
This code can not be run as is. Can you send complete code that can be run, or can you simplify the code to only contain the relevant plotting commands?
Cameron Brown
Cameron Brown 2016년 2월 25일
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?

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

답변 (1개)

matt dash
matt dash 2014년 11월 22일
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.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by