one of the colorbars is going out of the figure window

Technical Source
2 min readJun 12, 2023

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

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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.

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.