2015-11-18 22 views
4

如上所述,我想放大一個用plotyy創建的圖形。 當我這樣做時,yTicks不會更新到可見的新限制。 所以可能發生,如果你縮小太多,你沒有看到任何yTicks。 我發現ActionPostCallback函數,但我沒有得到它的工作,而xTicks罰款。Matlab:當我放大一個plotyy圖形時,yTicks不會自動更新

代碼:在

figure, plotyy(1:10,1:2:20,1:10,1:0.5:5.5) 

結果:

enter image description here

+0

來完成編輯我的第一篇文章 – TAK

回答

2

出於某種原因plotyy默認設置的軸的 'Ytickmode' 爲手動。

由於plotyy爲兩個數據集創建了兩組軸,因此設置每個軸的「Ytickmode」應解決此問題。

這可以通過

AX=plotyy(...) %this will create an axis with 2 elements one for each axis 
AX(1).YTickMode='auto'; 
AX(2).YTickMode='auto'; 
+1

哈,好的 - 這就是它! – marsei

+0

這個解決方案最適合我,因爲我在我的圖中有子圖。非常感謝。 – TAK

+2

順便說一句,不需要兩次調用AX。看我的編輯。 – marsei

3

由於這種行爲似乎並不存在於 '正常' plot調用,這看起來像一個內部錯誤通過plotyy創建軸對象。作爲一種選擇,您可以將多個軸疊加在一起,因此可以利用「默認」(缺少更好的單詞)縮放行爲。這種方法還可以讓您獨立完全控制兩個軸的行爲並避免看起來很多的plotyy的缺陷。

我已經稍微改編了one of my previous answers作爲這種情況的一個例子。

% Sample data 
x = 1:10; 
y1 = 1:2:20; 
y2 = 1:0.5:5.5; 

% Create axes & store handles 
h.myfig = figure; 
h.ax1 = axes('Parent', h.myfig, 'Box', 'off'); 

if ~verLessThan('MATLAB', '8.4') 
    % MATLAB R2014b and newer 
    h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right'); 
else 
    % MATLAB 2014a and older 
    ax1position = get(h.ax1, 'Position'); 
    h.ax2 = axes('Parent', h.myfig, 'Position', ax1position, 'Color', 'none', 'YAxisLocation', 'Right'); 
end 

% Preserve axes formatting 
hold(h.ax1, 'on'); 
hold(h.ax2, 'on'); 

% Plot data 
plot(h.ax1, x, y1, 'b'); 
plot(h.ax2, x, y2, 'g'); 

linkaxes([h.ax1, h.ax2], 'x'); 

和樣本圖像:

yay

請注意,我只掛了x,但你可以用linkaxes呼叫鏈路既xy軸。

4

您可能需要將YTickMode屬性設置爲auto

h = plotyy(1:10,1:2:20,1:10,1:0.5:5.5) 
set(h, 'YTickMode','Auto') 

enter image description here

完整代碼:

figure 

subplot(121) 
h1 = plotyy(1:10,1:2:20,1:10,1:0.5:5.5) 
title('Original') 

subplot(122) 
h2 = plotyy(1:10,1:2:20,1:10,1:0.5:5.5) 
title('Zoom') 
set(h2, 'YTickMode','Auto') 
+0

你需要'plotyy'的第一輸出獲取和使用的軸手柄,'gca'只能修改一個軸。 – excaza

+0

這隻適用於一方,但是這是潛在的問題。 – nivag

+0

是的 - 只是看到了!編輯應該這樣做。謝謝。 – marsei

相關問題