2013-10-24 376 views
8

我正在尋找一種方法來在y軸上使用不同的縮放比例在'contourf'生成的顯示頂部疊加一個x-y時間序列,用'plot'創建。在Matlab繪圖中重疊兩個軸

似乎在兩個xy圖的情況下這樣做的典型方式是使用內置函數'plotyy',它甚至可以由'plot'以外的函數驅動(如'loglog' ),只要輸入參數保持不變(x,y)。但是,因爲在我的情況下,contourf需要三個輸入參數,'plotyy'似乎不適用。下面是一些示例代碼,描述我想怎樣做:

x1 = 1:1:50; 
y1 = 1:1:10; 
temp_data = rand(10,50); 
y2 = rand(50,1)*20; 
figure; hold on; 
contourf(x1,y1,temp_data); 
colormap('gray'); 
plot(x1,y2,'r-'); 

理想情況下,我想時間序列(X1,Y2)有在右側顯示了自己的y軸,並可以擴展到相同的垂直範圍作爲contourf圖。

謝謝你的時間。

+0

你可能在這個問題找到答案:http://stackoverflow.com/questions/11531762/matlab-multiple-x-axis -one-below-another – Dan

+0

+1用於發佈可複製的代碼 –

+0

[This post](http://stackoverflow.com/questions/1719048/plotting-4-curves-in-a-single-plot-with-3-y - 軸)可能是你需要的。 – chappjc

回答

6

我不認爲有一個「乾淨」的方式來做到這一點,但你可以通過將兩個軸重疊在一起來僞造它。

x1 = 1:1:50; 
y1 = 1:1:10; 
temp_data = rand(10,50); 
y2 = rand(50,1)*20; 
figure; 
contourf(x1, y1, temp_data); 
colormap('gray'); 
h_ax = gca; 
h_ax_line = axes('position', get(h_ax, 'position')); % Create a new axes in the same position as the first one, overlaid on top 
plot(x1,y2,'r-'); 
set(h_ax_line, 'YAxisLocation', 'right', 'xlim', get(h_ax, 'xlim'), 'color', 'none'); % Put the new axes' y labels on the right, set the x limits the same as the original axes', and make the background transparent 
ylabel(h_ax, 'Contour y-values'); 
ylabel(h_ax_line, 'Line y-values'); 

事實上,這種「陰謀疊加」幾乎是肯定的plotyy函數在內部做了什麼。

這裏的例子輸出(I增加了可讀性的字體大小): overlaid axes