2015-03-03 159 views

回答

4

這是一種方法來做到這一點。你可以自定義它,但這應該會讓你走。

首先創建一個座標軸,並在圖形內改變它的位置/大小,向上移動它以爲第二個座標軸留出空間,並移除不需要的x和y標籤。然後創建一個指定位置/尺寸的第二根軸,使其適合第一根以下。

示例代碼:

clear 
clc 

%// Generate dummy data 
x = 1:2:100; 
y1 = rand(1,numel(x)); 

figure; 

%// Make an axes and set its position 
haxes1 = axes('Position',[.1 .1 .8 .7],'Color',[1 1 1]) 

%// Plot 1st curve 
plot(x,y1,'Parent',haxes1) 

%// Remove box and labels 
box off 
set(gca,'XTickLabel','','XTick',[],'YTick',[]) 

hold on 

%// Get current axes position. You set it so you could get the parameters 
%// directly as well. 
axes1Pos = get(gca,'Position'); 

%// Shift 1st axes upward 
set(gca,'Position',[axes1Pos(1) 2.6*axes1Pos(2) axes1Pos(3) axes1Pos(4)]) 

%// Change the poisition/size of the 2nd axes to fit below the 1st one 
haxes2 = axes('Position',[axes1Pos(1) axes1Pos(2)/2.5 axes1Pos(3) axes1Pos(4)/2.5]) ; 

%// Use linspace to generate colored points to use with scatter. 
c = linspace(1,10,length(x)); 

%// Add 2nd plot and keep only x label 
scatter(x,rand(1,numel(x)),40,c,'filled') 
set(gca,'YTick',[]) 

box off 

%// Place a ylabel for both axes 
text(-4, 1.7,'Super nice y label','rotation',90,'FontSize',16,'HorizontalAlignment','center') 

輸出示例:

enter image description here

還有其他的方法可以做到這一點。

希望有幫助!

+0

謝謝,我會試試:) – Raldenors 2015-03-03 14:25:12

+0

好!如果有什麼不起作用,請問! – 2015-03-03 14:42:02