Search code examples
matlabresizematlab-figureaxiscolorbar

Secondary axes get displaced when resizing figure


I am trying to plot a Matlab (R2017a) figure (filled contour + colorbar) with two X axes, on the bottom and top of the graph, with the same scale but different tick marks and labels. Following the suggestions found here and here, I have achieved it, but when I try to resize the figure window manually or to print it setting certain proportions different from the default ones, e.g.:

set(gcf,'PaperUnits','centimeters','PaperPosition',[0 0 30 15])
print(gcf,'-dpng',path,'-r300')

the new axes get displaced:

correct axes

displaced axes

I have reproduced my issue with the peaks example data from Matlab:

contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
    'XAxisLocation','top',...
    'YAxisLocation','Right',...
    'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
    'YTick', get(ax1, 'YTick'), 'YTickLabels', []);

Curiously enough, if I remove the colobar command and plot just the filled contour, the figure behaves correctly:

Filled contour with no colorbar

Does anyone know why is this happening (and how could it be solved)? I'm also open to achieve the two-X-axis plot by other means.


Solution

  • Your problem is that one axis has a colorbar and other does not, and even if you would add a colorbar to both axis, there can be quite a lot of automatic things going on that would resize your axis differently.

    However, we can add an event listener and define a function to operate make both axis the same. The listener will make sure it catches the event (resizing) and that it calls the function we define. This is the code I made for that:

    %% this creates the listener for change of size in the figure
    f = figure('SizeChangedFcn',@(src,evn) fixaxis(src));
    %% this is your code
    contourf(peaks)
    ax1=gca;
    colorbar
    set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
    ax1_pos = ax1.Position; % position of first axes
    ax2 = axes('Position',ax1_pos,... % set the new pair of axes
        'XAxisLocation','top',...
        'YAxisLocation','Right',...
        'Color','none');
    set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
    set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
        'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
    
    %% this will resize the axis if 2 of them exist
    function fixaxis(src)
      ax=findall(src,'Type','Axes');
      if length(ax)==2
      ax(2).Position=ax(1).Position;
      end
    end