Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对齐MATLAB子地块?_Matlab_Matlab Figure_Subplot - Fatal编程技术网

如何对齐MATLAB子地块?

如何对齐MATLAB子地块?,matlab,matlab-figure,subplot,Matlab,Matlab Figure,Subplot,我的MATLAB子图没有完全对齐,请参见下图中的绿色矩形。如何避免这种转变 这就是我正在尝试的 clear all; clc; RF=RF_data(:,2:end); figure subplot(3,4,1) pos_pre = 1.3:1:3.3; % for positioning boxplot(RF(2:71,[45,47,46]),... 'colors','b','positions',pos_pre,'width',0.18,... 'symbo

我的MATLAB子图没有完全对齐,请参见下图中的绿色矩形。如何避免这种转变

这就是我正在尝试的

clear all;
clc;

RF=RF_data(:,2:end);

figure

subplot(3,4,1)

pos_pre = 1.3:1:3.3;    % for positioning

boxplot(RF(2:71,[45,47,46]),...
    'colors','b','positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '}) 
hold on  

pos_post = 1.55:1:3.55;   % Ignore this comment

boxplot(RF(72:end,[45,47,46]),'colors','r',...
    'positions',pos_post,'width',0.18,'symbol', 'rd','OutlierSize',3,...
    'factorseparator',1); 

set(gca,'XTick',1.5:1:3.5)  
set(gca,'XTickLabel',{'10a', '10b', '10c'}, 'FontSize', 7)

X = get(gca,'XLim');  
Y = get(gca,'YLim');
hold off 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(3,4,2)

pos_pre = 1.3:1:3.3;   % Ignore this comment

boxplot(RF(2:71,[17,16,15]),...
    'colors','b','positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '})  
hold on  

pos_post = 1.55:1:3.55;  % Ignore this comment

boxplot(RF(72:end,[17,16,15]),'colors','r',...
    'positions',pos_post,'width',0.18,'symbol', 'rd','OutlierSize',3,...
    'factorseparator',1); 

set(gca,'XTick',1.5:1:3.5) 
set(gca,'XTickLabel',{'7a', '7b', '7c'}, 'FontSize', 7) 

hold off 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(3,4,3)

pos_pre = 1.3:1:3.3;  % Ignore this comment

boxplot(RF(2:71,[54, 53, 52]),'colors','b',...
    'positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '})  
hold on  

pos_post = 1.55:1:3.55;  

boxplot(RF(72:end,[54, 53, 52]),'colors','r',...
    'positions',pos_post,'width',0.18,'symbol', 'rd','OutlierSize',3,...
    'factorseparator',1); 

set(gca,'XTick',1.5:1:3.5)  
set(gca,'XTickLabel',{'6a', '6b', '6c', '6d', '6e', '6f', '6g'}, 'FontSize', 7)

hold off 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(3,4,4)

pos_pre = 1.3:1:3.3;  

boxplot(RF(2:71,[76,75,74]),...
    'colors','b','positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '})  
hold on  

pos_post = 1.55:1:3.55;  

boxplot(RF(72:end,[76,75,74]),'colors','r',...
    'positions',pos_post,'width',0.18,'symbol', 'rd','OutlierSize',3,...
    'factorseparator',1); 

set(gca,'XTick',1.5:1:3.5)  
set(gca,'XTickLabel',{'5a', '5b', '5c', '5d', '5e', '5f', '5g', '5h'}, 'FontSize', 7)

X = get(gca,'XLim');  
Y = get(gca,'YLim');
hold off 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,4,(5:6))

pos_pre = 1.3:1:8.3;  

boxplot(RF(2:71,[32,31,30,50,62,85,86,34]),...
    'colors','b','positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '})  
hold on  

pos_post = 1.55:1:8.55;  

boxplot(RF(72:end,[32,31,30,50,62,85,86,34]),'colors','r',...
    'positions',pos_post,'width',0.18,'symbol', 'rd','OutlierSize',3,...
    'factorseparator',1); 

set(gca,'XTick',1.5:1:8.5)
set(gca,'XTickLabel',{'5a', '5b', '5c', '5d', '5e', '5f', '5g', '5h'}, 'FontSize', 7)

hold off 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(3,4,(7:8))

pos_pre = 1.3:1:7.3;  

boxplot(RF(2:71,[12, 11, 42, 41, 40, 79, 78]),...
    'colors','b','positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '})  
hold on  

pos_post = 1.55:1:7.55;  

boxplot(RF(72:end,[12, 11, 42, 41, 40, 79, 78]),'colors','r',...
    'positions',pos_post,'width',0.18,'symbol', 'rd','OutlierSize',3,...
    'factorseparator',1); 

set(gca,'XTick',1.5:1:7.5) 
set(gca,'XTickLabel',{'6a', '6b', '6c', '6d', '6e', '6f', '6g'}, 'FontSize', 7)

hold off 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,4,(9:12))

pos_pre = 1.3:1:19.3;  

boxplot(RF(2:71,[1,63,89,88,87,27,80,33,26,28,39,9,21,72,77,19,18,38,5]),...
    'colors','b','positions',pos_pre,'width',0.18,...
    'symbol', 'bd', 'OutlierSize',3);
set(gca,'XTickLabel',{' '})  
hold on  

pos_post = 1.55:1:19.55;  

boxplot(RF(72:end,[1,63,89,88,87,27,80,33,26,28,39,9,21,72,7,19,18,38,5]),...
    'colors','r','positions',pos_post,'width',0.18,'symbol', 'rd',...
    'OutlierSize',3,'factorseparator',1); 

set(gca,'XTick',1.5:1:19.5)
set(gca,'XTickLabel',{'2a', '2b', '2c', '2d', '2e', '2f', '2g','2e','2f','2g','2h',...
    '2i','2j','2k','2l','2m','2n','2o','2p','2q','2r','2s'}, 'FontSize', 7)


X = get(gca,'XLim');  
Y = get(gca,'YLim');
hold off 

text('Position',[0.85*X(1,2), 0.9*Y(1,2)],'String','1901-1970',...
    'color', 'b', 'FontWeight', 'bold', 'FontSize', 8) 

text('Position',[0.85*X(1,2), 0.8*Y(1,2)],'String','1971-2013',...
    'color', 'r', 'FontWeight', 'bold', 'FontSize', 8) 

%%
disp('Done')

它要求添加更多的细节,所以我只是随意写一些东西

在这个例子中,我看不出有什么问题:

% first row
subplot(3,4,1), plot(cumsum(randn(100,1)))
subplot(3,4,2), plot(cumsum(randn(100,1)))
subplot(3,4,3), plot(cumsum(randn(100,1)))
subplot(3,4,4), plot(cumsum(randn(100,1)))

% second row
subplot(3,2,3), plot(cumsum(randn(100,1)))
subplot(3,2,4), plot(cumsum(randn(100,1)))

% third row
subplot(3,1,3), plot(cumsum(randn(100,1)))


编辑: 使用您设定的代码,快速解决方案是手动调整要对齐的轴位置

首先,您需要存储所有子地块的句柄。因此,请在脚本中添加以下内容:

figure
h = zeros(1,7);

h(1) = subplot(3,4,1);
% ... [snip] ...

h(2) = subplot(3,4,2);
% ... [snip] ...

% ...

h(7) = subplot(3,4,(9:12));
% ... [snip] ...
然后你可以这样做:

% positions of axes on the left
positions = cell2mat(get(h([1 5 7]), 'Position'));

% minimum x-position
x = min(positions(:,1));

% set them to be left aligned, and just the width accordingly
for i=[1 5 7]
    pos = get(h(i), 'Position');
    set(h(i), 'Position',[x, pos(2), pos(3) + (pos(1) - x), pos(4)]);
end
下面是我得到的结果(我使用随机数据
RF_data=rand(100100);
运行脚本):


我看不出这个例子有什么问题:

% first row
subplot(3,4,1), plot(cumsum(randn(100,1)))
subplot(3,4,2), plot(cumsum(randn(100,1)))
subplot(3,4,3), plot(cumsum(randn(100,1)))
subplot(3,4,4), plot(cumsum(randn(100,1)))

% second row
subplot(3,2,3), plot(cumsum(randn(100,1)))
subplot(3,2,4), plot(cumsum(randn(100,1)))

% third row
subplot(3,1,3), plot(cumsum(randn(100,1)))


编辑: 使用您设定的代码,快速解决方案是手动调整要对齐的轴位置

首先,您需要存储所有子地块的句柄。因此,请在脚本中添加以下内容:

figure
h = zeros(1,7);

h(1) = subplot(3,4,1);
% ... [snip] ...

h(2) = subplot(3,4,2);
% ... [snip] ...

% ...

h(7) = subplot(3,4,(9:12));
% ... [snip] ...
然后你可以这样做:

% positions of axes on the left
positions = cell2mat(get(h([1 5 7]), 'Position'));

% minimum x-position
x = min(positions(:,1));

% set them to be left aligned, and just the width accordingly
for i=[1 5 7]
    pos = get(h(i), 'Position');
    set(h(i), 'Position',[x, pos(2), pos(3) + (pos(1) - x), pos(4)]);
end
下面是我得到的结果(我使用随机数据
RF_data=rand(100100);
运行脚本):


这非常有效!您是如何创建这些子地块的?一些代码复制请…这是伟大的作品!您是如何创建这些子地块的?请复制一些代码。通过设置轴的“位置”属性,您可以随时完全控制轴的位置。。您甚至不需要调用
子批
,只需创建常规的
。您的方法没有帮助。我得到了同样的图,同样的间距。但是,如果我复制粘贴您的代码,它与您显示的代码完全相同。好的,我在您的代码中看到了问题。。一个快速的解决方法是抓住有问题的轴,轻推它们的位置使其对齐。给我一点时间,我会添加一个示例代码。好极了,伙计。它工作得非常好。非常感谢@Mario在脚本末尾调用它:
set(h,{'Position'},get(h,'Position'))
通过设置轴的“Position”属性,您始终可以完全控制轴的位置。。您甚至不需要调用
子批
,只需创建常规的
。您的方法没有帮助。我得到了同样的图,同样的间距。但是,如果我复制粘贴您的代码,它与您显示的代码完全相同。好的,我在您的代码中看到了问题。。一个快速的解决方法是抓住有问题的轴,轻推它们的位置使其对齐。给我一点时间,我会添加一个示例代码。好极了,伙计。它工作得非常好。非常感谢@Mario在脚本末尾调用它:
set(h,{'Position'},get(h,'Position'))