one of the colorbars is going out of the figure window
30 次查看(过去 30 天)
显示 更早的评论
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1401994/image.png)
0 个评论
采纳的回答
Benjamin Kraus
2023-6-7
编辑:Benjamin Kraus
2023-6-7
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'});
更多回答(1 个)
Govind KM
2023-6-5
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!