one of the colorbars is going out of the figure window

Illustration
Santhosh Krishnamurthy - 2023-06-12T12:37:26+00:00
Question: one of the colorbars is going out of the figure window

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);  

Expert Answer

Profile picture of Neeta Dsouza 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'.

By default, the axes locks the OuterPosition at [0 0 1 1] so that nothing extends beyond the edges of the figure. It then calculates how much space is required for things like colorbars and labels, and makes the Position smaller until there is enough room. This is the default behavior, and the behavior when PositionConstraint is equal to 'outerposition'.
 
When you set the Position property (indrectly via linkprop), the effect is that you are telling MATLAB "please don't automatically adjust the (inner) position of my axes". Because you've told MATLAB not to move the white part of the axes, instead MATLAB positions the colorbars and other decorations outside the axes, in whatever space is available. The result is that there is not enough room for the colorbar, and it is clipped. This is the behavior when PositionConstraint is equal to 'innerposition', which is what happens when you set the Position property.
 
Fortunately, there is a fix: use tiledlayout to both position your colorbars and keep your axes aligned with one another.
 
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)');

 

colorbars-is-going-out-of-the-figure-window

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'});

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!