I am trying to bring two pcolor plots on top of each other with different colormaps. One of the colorbars goes out of the figure window, have tried placing the colorbar in other places like southoutside or westoutside and the same problem persists. Is there any better way to do it? Any help is appreciated. f = figure; ax = gca; ax(2) = copyobj(ax, ax.Parent); linkprop([ax(1), ax(2)], {'XLim', 'YLim','Position', 'View'}); p = pcolor(ax(1), xc, yc, e); set(p, 'EdgeColor', 'none', 'FaceAlpha', 1); set(ax(1), 'Colormap', bone); cb(1) = colorbar(ax(1), 'eastoutside'); p2 = pcolor(ax(2), xc, yc, d); set(p2, 'EdgeColor', 'none', 'FaceAlpha', 0.5); set(ax(2), 'Colormap', copper); cb(2) = colorbar(ax(2), 'northoutside'); ax(2).Visible = 'off'; ax(1).XAxis.FontSize = 14; ax(1).YAxis.FontSize = 14; ax(2).XAxis.FontSize = 14; ax(2).YAxis.FontSize = 14; ax(1).FontSize = 14; ax(2).FontSize = 14; xlabel('x(\mum)'); ylabel('y(\mum)'); %exportgraphics(gcf, 'trial.png', 'Resolution', 300);
Neeta Dsouza answered .
2025-11-20
The issue you are running into is due to this line of code:
linkprop([ax(1), ax(2)], {'XLim', 'YLim','Position', 'View'});
That line of code is setting the Position property on the axes, which switches the PositionConstraint from 'outerposition' to 'innerposition'.
f = figure; t = tiledlayout(1,1); ax(1) = axes(t); p = pcolor(ax(1), peaks); set(p, 'EdgeColor', 'none', 'FaceAlpha', 1); set(ax(1), 'Colormap', bone); cb(1) = colorbar(ax(1)); cb(1).Layout.Tile = 'east'; ax(2) = axes(t); p2 = pcolor(ax(2), peaks); set(p2, 'EdgeColor', 'none', 'FaceAlpha', 0.5); set(ax(2), 'Colormap', copper); cb(2) = colorbar(ax(2)); cb(2).Layout.Tile = 'north'; ax(2).Visible = 'off'; ax(1).XAxis.FontSize = 14; ax(1).YAxis.FontSize = 14; ax(2).XAxis.FontSize = 14; ax(2).YAxis.FontSize = 14; ax(1).FontSize = 14; ax(2).FontSize = 14; xlabel(ax(1),'x(\mum)'); ylabel(ax(1),'y(\mum)');

Note: When you call linkprop, you have to store the output handle, otherwise the link will be broken immediately. The output from linkprop is a link object that "owns" the link. If you don't store that object, then the link object will be stored in ans, but that will be cleared as soon as the next command runs. Once that link object is cleared, the link will be broken.
link = linkprop([ax(1), ax(2)], {'XLim', 'YLim','View'});