필터 지우기
필터 지우기

one of the colorbars is going out of the figure window

조회 수: 27 (최근 30일)
nash-host
nash-host 2023년 6월 3일
댓글: nash-host 2023년 6월 8일
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);

채택된 답변

Benjamin Kraus
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
nash-host
nash-host 2023년 6월 8일
Wow, thanks a lot Benjamin!
The issue is fixed now, thank you so much. Your answer not only helped in fixing the issue but also helped me understand the issue properly. Very nice of you :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Govind KM
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);
  댓글 수: 1
nash-host
nash-host 2023년 6월 7일
Hi Govind,
Many thanks for your response. I did try your suggestion but still there is no change. Manual positions are being applied to only one of the colorbars.
I suspect this issue is occuring mainly because of ax(2).Visible = 'off'
But I have to use it in order to plot one pcolor plot on top of another. Any other possibilities?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by