我寫了一個函數,應該做你想要的。它保持在完全相同的大小和位置的軸,它使X標記下來,並增加了數字大小要足夠大,以顯示標籤:
function moveLabel(ax,offset,hFig,hAxes)
% get figure position
posFig = get(hFig,'Position');
% get axes position in pixels
set(hAxes,'Units','pixels')
posAx = get(hAxes,'Position');
% get label position in pixels
if ax=='x'
set(get(hAxes,'XLabel'),'Units','pixels')
posLabel = get(get(hAxes,'XLabel'),'Position');
else
set(get(hAxes,'YLabel'),'Units','pixels')
posLabel = get(get(hAxes,'YLabel'),'Position');
end
% resize figure
if ax=='x'
posFigNew = posFig + [0 -offset 0 offset];
else
posFigNew = posFig + [-offset 0 offset 0];
end
set(hFig,'Position',posFigNew)
% move axes
if ax=='x'
set(hAxes,'Position',posAx+[0 offset 0 0])
else
set(hAxes,'Position',posAx+[offset 0 0 0])
end
% move label
if ax=='x'
set(get(hAxes,'XLabel'),'Position',posLabel+[0 -offset 0])
else
set(get(hAxes,'YLabel'),'Position',posLabel+[-offset 0 0])
end
% set units back to 'normalized' and 'data'
set(hAxes,'Units','normalized')
if ax=='x'
set(get(hAxes,'XLabel'),'Units','data')
else
set(get(hAxes,'YLabel'),'Units','data')
end
end
在這種情況下offset
應該是絕對偏移像素。如果你想要相對偏移量,我認爲這個函數很容易被重寫。 hFig
是圖柄和hAxes
的軸柄。
編輯:在調用函數之前,使用hFig = figure;
和軸創建圖形hAxes = axes;
(然後像在問題中設置軸那樣設置軸:set(hAxes,...)
)。
EDIT2:增加了的和XLabel
分別變回「歸一化」和「數據」的行。這樣,這個數字在調整大小後保持您想要的樣子。
EDIT3:修改了X和Y標籤的功能。其他輸入ax
應該是'x'
或'y'
。
解決y軸有什麼顯着的區別?在這種情況下,我想情節本身需要移動,對吧? – Niko 2013-02-25 09:42:49
我認爲代碼很容易轉換爲在y軸上工作。我現在正在乘公共汽車,所以現在不能這樣做,但我會在今晚晚些時候(幾個小時內)看看它。 – ThijsW 2013-02-25 09:54:15
由於某種原因,此方法無法通過例如。在x軸上有50個像素偏移量。在我的設置中,標籤然後被切成兩半。然而,在幾個像素的理想範圍內,這個功能就像一個魅力,所以非常感謝你! – Niko 2013-02-25 17:11:58