2012-03-12 210 views
4

我運行此代碼創建的現有線路:上面繪製一條線/下面plotyy

t = linspace(0, 10, 1000); 
y1 = 2*t; 
y2 = 3*t; 
figure; 
[ax, h1, h2] = plotyy(t, y1, t, y2); 
set(h1, 'LineWidth', 4); 
set(h2, 'LineWidth', 4); 
hold on; 
h3 = plot([5, 5], [0, 3000], 'LineWidth', 6, 'Color', [0.6, 0.6, 0.6]); 

它創建這個情節:

enter image description here

注意如何垂直灰線出現在藍線(y1)的頂端,但低於綠線(y2)。

我該如何在其他兩條線的上方或其他兩條線的下方繪製灰線?

+1

好像'plotyy'增添了不少複雜的這個問題,我發現這個http://www.mathworks.com/matlabcentral/newsreader/view_thread/292321與此http:// WWW .mathworks.com/matlabcentral/newsreader/view_thread/311595,但沒有好的解決方案。 – macduff 2012-03-12 20:40:20

回答

3

我看到兩個選項:

A.把灰線向前通過將其移動到由plotyy命令

set(h3,'parent',ax(2)); 

B.放置在通過重新排列底部的灰線創建的第二軸線軸上 上的藍色和灰色線的順序。

chld = [h1 h3]; 
set(ax(1),'children',chld); %# reorders the two lines so that the gray line is in back. 
2

要使灰線底部,您還可以更改繪圖的順序。

t = linspace(0, 10, 1000); 
y1 = 2*t; 
y2 = 3*t; 
figure; 
h3 = plot([5, 5], [0, max(y1)], 'LineWidth', 6, 'Color', [0.6, 0.6, 0.6]); 
hold on; 
[ax, h1, h2] = plotyy(t, y1, t, y2); 
set(h1, 'LineWidth', 4); 
set(h2, 'LineWidth', 4); 

儘管如此,h3 = plot(...)中有一個技巧可以確保左標度是正確的。

enter image description here