放置颜色条后如何保持子图大小不变 [英] How to keep the subplot sizes unchanged after putting a colorbar

查看:120
本文介绍了放置颜色条后如何保持子图大小不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有一个1 x 2子图,并在其中绘制了一些图形,如下所示:

Let us say we have a 1-by-2 subplot and we plot some graphics inside as follows:

subplot(1,2,1)
surf(peaks(20))

subplot(1,2,2)
surf(peaks(20))

然后我们要放置一个颜色栏:

And then we want to put a colorbar:

colorbar

我不希望结果压缩正确的身材.我们如何才能将颜色条从最右边的图形放到一行子图中,并使它们的大小保持不变?

I don't want the right figure squezzed as in the result. How can we put the colorbar out of the rightmost figure in a row of subplots and keep the sizes of them unchanged?

注意:实际上,我需要它用于在颜色条很常见的地方绘制图像,并且我想将其放在右边.为了简单起见,我使用了这个玩具示例.

Note: Actually, I need it for plotting images where the colorbar is common and I want to put it on the right. I used this toy example for simplicity.

推荐答案

您可以提取第一个图的位置并在第二个图上使用.重新缩放时,MATLAB会自动将颜色条移到右侧.

You could just extract the position of the first plot and use on the second. MATLAB automatically moves the colorbar to the right when rescaling.

f1=figure(1);clf;
s1=subplot(1,2,1);
surf(peaks(20));

s2=subplot(1,2,2);
surf(peaks(20));
hb = colorbar('location','eastoutside');

%% # Solution:
s1Pos = get(s1,'position');
s2Pos = get(s2,'position');
s2Pos(3:4) = [s1Pos(3:4)];
set(s2,'position',s2Pos);



%% # Alternative method. Brute force placement
set(s1,'Units','normalized', 'position', [0.1 0.2 0.3 0.6]);
set(s2,'Units','normalized', 'position', [0.5 0.2 0.3 0.6]);
set(hb,'Units','normalized', 'position', [0.9 0.2 0.05 0.6]);

这篇关于放置颜色条后如何保持子图大小不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆