Search code examples
matlabmatlab-figureaxis-labelsfigure

Top x axis' labels outside of figure (MWE)


The label of the top x axis appears outside of the figure, how can this be fixed? Also, for some reason I don't understand, "hold on + plot()" doesn't work in this case, I would like to be able to plot a vertical line but this doesn't appear on the figure either

figure1=figure(4)
axes1 = axes('Parent',figure1,'YScale','log','XScale','log','Layer','top');
grid(axes1,'on');
hold(axes1,'on');
[C,h]=contourf(peaks,[10],'LineColor','none');
clabel(C,h);
hold on %doesn't work
plot([10 10],[0 10],'--k','LineWidth',2) %doesn't work
axis tight;    
axis([1 50 1 50]) 
xlabel('\lambda_x','Fontsize',20);
ylab=ylabel('y^+','Fontsize',20);
grid off
set(ylab, 'Units', 'Normalized', 'Position', [-0.1, 0.5, 0]);
set(gca,'linewidth',1.5,'FontSize',16)
colormap(flipud(gray(256)));
colorbar;

ax2 = axes('Position',axes1.Position,'YScale','log','XScale','log','XAxisLocation','top','YAxisLocation','right','Color','none','YTick',[]);
xla2=xlabel(ax2,'\lambda_x^+','Fontsize',20);
axis(ax2,[1*100 50*100 1*100 50*100]) 
set(ax2,'linewidth',1.5,'FontSize',16)
% Top x axis' label appears outside of figure

enter image description here


Solution

  • The missing vertical line is very easy to fix. The problem is that you are trying to plot 0 on a log scale, which cannot happen. If you change

    plot([10 10],[0 10],'--k','LineWidth',2)
    

    to

    plot([10 10],[0.01 10],'--k','LineWidth',2)
    

    you will get the line to show up. An even better option is to use the xline command like this:

    xline(10, '--k', 'LineWidth', 2)
    

    The second problem (missing x axis label on top) was harder to fix. The reason the top xlabel was disappearing is the the size of the second axes was set to be the same size as the first axes (which has the label on the bottom) so there was no room for a label on top and it would get placed off the figure. Normally when you add a label, the axes will move inward to accommodate the label, but since you set the size of the axes manually, it didn't get moved.

    I created some code that does what you want. I started by choosing the axes position manually (through trial and error), this leaves enough room for the labels. I also positioned the color bar manually, if it gets positioned automatically, it alters the axes position, which we didn't want since I am setting them manually. I also eliminated some unnecessary code for turning grids on and off and using hold. I also eliminated the code for creating the first axes since the contourf command will create an axis. I also combined the axis adjusting commands into the command for creating the second axes.

    Here is the final code and the figure that results

    figure(4)
    clf
    ax_position = [0.15, 0.175, 0.73, 0.625]; % x, y, width and height in normalized units
    [C,h]=contourf(peaks, 10, 'LineColor', 'none');
    set(gca, 'YScale', 'log', 'XScale', 'log', 'XLim', [1, 50], 'YLim', [1, 50], 'linewidth', 1.5, 'FontSize', 16, ...
        'Position', ax_position)
    xline(10, '--k', 'LineWidth', 2)
    xlabel('\lambda_x','Fontsize',20);
    ylab=ylabel('y^+','Fontsize',20);
    colormap(flipud(gray(256)));
    h_cb = colorbar('Position', [0.9, ax_position(2), 0.0381, ax_position(4)]);
    
    ax2 = axes('Position', ax_position, 'YScale', 'log', 'XScale', 'log', 'XAxisLocation', 'top', ...
               'YAxisLocation', 'right', 'Color', 'none', 'YTick',[], 'linewidth', 1.5, 'FontSize',16, ...
               'XLim', 100*[1, 50], 'YLim', 100*[1, 50]);
    xlabel(ax2,'\lambda_x^+','Fontsize',20);
    

    enter image description here