one of the colorbars is going out of the figure window
29 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
Benjamin Kraus
on 7 Jun 2023
Edited: Benjamin Kraus
on 7 Jun 2023
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)');
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'});
More Answers (1)
Govind KM
on 5 Jun 2023
Hi Santhosh,
You can set the location of the colorbar manually using the Position property i.e instead of setting the location as 'eastoutside' or 'northoutside', you can specify a custom location and size, represented as a four-element vector of the form [left, bottom, width, height]. The left and bottom elements specify the distance from the lower-left corner of the figure or to the lower-left corner of the colorbar. The width and height elements specify the dimensions of the colorbar. The position is specified in normalized coordinates (0 to 1).
Considering cbar as the handle to the colorbar, example code to set the location manually is as follows:
customPosition = [0.6, 0.2, 0.05, 0.6];
set(cbar, 'Position', customPosition);
See Also
Categories
Find more on Purple in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!