2016-07-07 117 views
4

我發現位於軸上或靠近軸的數據點很難看清。當然,明顯的解決辦法是簡單地使用axis([xmin xmax ymin ymax])來改變繪圖區域,但這在所有情況下都不是優選的;例如,如果x軸是時間,那麼將最小x值移動到-1以在0處顯示活動是沒有意義的。從MATLAB的繪圖區域分離軸線

相反,我希望能簡單地移動X和Y軸從繪圖區域了,像我這樣做這裏: left: Matlab generated, right: desired (image editing software) 左:MATLAB產生,右:期望(圖像編輯軟件)

是有一種方法可以自動在MATLAB中做到這一點?我認爲有可能通過使用軸屬性outerposition(即將其設置爲[0 0 0.9 0.9]並在最初繪製新軸的位置繪製新軸?)來完成此操作,但我沒有采用該策略。

+0

另一個選項可能類似'axis([minx-minx/10 maxx miny maxy])'或某些其他因素除了1/10 – shamalaia

回答

4

這裏的答案已經向您展示了大部分的方法 - 這裏是按照您放在一起的示例分開x和y軸的最後一步。

f = figure ('color', 'white'); 
% create the axes and set some properties 
ax = axes ('parent', f, 'box', 'off', 'nextplot', 'add', 'XMinorTick', 'on', 'YMinorTick', 'on'); 
% plot some data 
plot (ax, 0:10, [0:10].^2, 'rx-') 
% modify the x and y limits to below the data (by a small amount) 
ax.XLim(1) = ax.XLim(1)-(ax.XTick(2)-ax.XTick(1))/4; 
ax.YLim(1) = ax.YLim(1)-(ax.YTick(2)-ax.YTick(1))/4; 
% Set the tick direction 
ax.TickDir = 'out'; 
% draw the plot to generate the undocumented vertex data var 
drawnow() 

%% R2015a 
% X, Y and Z row of the start and end of the individual axle. 
ax.XRuler.Axle.VertexData(1,1) = 0; 
ax.YRuler.Axle.VertexData(2,1) = 0; 


%% R2015b 
% extract the x axis vertext data 
% X, Y and Z row of the start and end of the individual axle. 
vd = get(ax.XAxis.Axle,'VertexData'); 
% reset the zero value 
vd(1,1) = 0; 
% Update the vertex data 
set(ax.XAxis.Axle,'VertexData',vd); 
% repeat for Y (set 2nd row) 
vd = get(ax.YAxis.Axle,'VertexData'); 
vd(2,1) = 0; 
set(ax.YAxis.Axle,'VertexData',vd); 

編輯:頂點是什麼,Matlab的再現每當軸/圖改變大小,或者如果你縮放或平移爲例。

通過添加一個偵聽器來嘗試捕獲它,您可以嘗試抵消這一點(請記住您在這裏使用未公開的功能)。我們可以使用被稱爲很多次的MarkedClean事件。

addlistener (ax, 'MarkedClean', @(obj,event)resetVertex(ax)); 

在那裏你resetVertex功能是一樣的東西:(R2015b後纔會顯示)

編輯2添加的代碼以關閉小的刻度在0以下

function resetVertex (ax) 
    % extract the x axis vertext data 
    % X, Y and Z row of the start and end of the individual axle. 
    ax.XAxis.Axle.VertexData(1,1) = 0; 
    % repeat for Y (set 2nd row) 
    ax.YAxis.Axle.VertexData(2,1) = 0; 

    % You can modify the minor Tick values by modifying the vertex data 
    % for them, e.g. remove any minor ticks below 0 
    ax.XAxis.MinorTickChild.VertexData(:,ax.XAxis.MinorTickChild.VertexData(1,:)<0) = []; 
    ax.YAxis.MinorTickChild.VertexData(:,ax.YAxis.MinorTickChild.VertexData(1,:)<0) = []; 
end 

enter image description here

注意:這使用未公開的功能 - >所以可能只適用於特定版本的Matlab(我添加了代碼對於r2015a & r2015b)和Matlab可能會重新創建頂點數據,具體取決於您對圖進行的操作。

+0

非常好!但它在2015a中不起作用,導致錯誤:'類'matlab.graphics.axis.Axes'沒有適當的方法,屬性或字段'XAxis'。 – EBH

+0

@EBH - 爲'2015a'添加了所需的代碼 – matlabgui

+0

我運行了你的代碼。它完美的作品。但是我看到一個我不明白的問題。當我最大化數字窗口時,即使我恢復數字窗口,軸再次連接並保持連接。我正在使用MATLAB R2016a。查看並放大此圖以更好地解釋我所說的內容http://i.stack.imgur.com/9svbc.jpg(您的解決方案雖然是最棒的):-) –

1

您可以從小於零開始創建座標軸,然後從圖中移除小於零的刻度。例如

plot(0:3:30,0:3:30);  %Some random data for plotting 
h = gca; 
axis([-1 30 -1 30]);  %Setting the axis from less than zero 
box off;     %Removing box 
h.TickDir = 'out';  %Setting Direction of ticks to outwards 
h.XTickLabel(1)= {' '}; %Removing the first tick of X-axis 
h.YTickLabel(1)= {' '}; %Removing the first tick of Y-axis 

有了這個代碼,你會得到這樣的結果:

zeromarkmaynotbeincluded

這可能有一個缺點,有時候,那零個蜱也可能會刪除(因爲你可以看到在上面的圖)。這是因爲該圖已經將軸的第一個滴答等於零。這可以使用if條件來避免。因此,可以將代碼如下修改:

plot(0:3:30,0:3:30); 
h = gca; 
axis([-1 30 -1 30]); 
box off; 
h.TickDir = 'out'; 

if str2num(cell2mat(h.XTickLabel(1))) <0 
    h.XTickLabel(1)= {' '}; 
end 

if str2num(cell2mat(h.YTickLabel(1))) <0 
    h.YTickLabel(1)= {' '}; 
end 

上面的代碼將產生以下結果: -

zeromarkincluded

還要注意的是,對於你的情況,因爲你的軸蜱是非常更少,-1可能不太適合軸的起始值,您可能需要使用-0.1而不是axis([-0.1 30 -0.1 30]);

2

以下是實現該操作的簡單方法:

% some data: 
x = 1:100; 
[email protected](x) 5.*x; 
y=f(x)+rand(1,length(x))*50; 
close all 
% plotting: 
f1 = figure('Color','white'); 
ax = axes; 
plot(ax,x,y,'o'); 
% 'clean' the data area a little bit: 
box off 
ax.TickDir = 'out'; 
% pushing the axis a bit forward: 
lims = axis; 
pos = ax.Position; 
axis([lims(1)-ax.XTick(2)/5 lims(2)+0.1 lims(3)-ax.YTick(2)/5 lims(4)+0.1]) 
% Create lines 
firstXtick = 0.013; %this value need to be adjusted only once per figure 
firstYtick = 0.023; %this value need to be adjusted only once per figure 
lx = annotation(f1,'line',[pos(1) pos(1)+firstXtick],... 
    [pos(2) pos(2)],'Color',[1 1 1],'LineWidth',1); 
ly = annotation(f1,'line',[pos(1) pos(1)],... 
    [pos(2) pos(2)+firstYtick],'Color',[1 1 1],'LineWidth',1); 

其中產量圖中: enter image description here

唯一在此進行調整,每一次式的人物,是firstXtickfirstYtick值,也必須進行微調,以特定軸。將它們設置爲正確的值後,可以調整圖形的大小而不會出現問題。縮放和平移需要一些修正。

+0

@achilles&Sardar_Usama我已經編輯了我的答案,以便在問題中以更接近結果的方式(以與matlabgui不同的方式),告訴我它是否適用於您 – EBH

+0

這也有訣竅 - 對不起,我不能接受2個答案... +1 – achilles

+0

@EBH帶有'annotation'的漂亮黑客!我已經投票了! –