2014-04-27 93 views
4

我正在嘗試將MATLAB的刻度線與我的網格對齊,但我無法找到抵消標籤的好方法。將MATLAB軸刻度移動了半步

此外,如果我運行set(gca,'XTickLabel',1:10),我的x刻度標籤結束範圍從1到5.什麼給?

enter image description here

回答

7

你需要移動後,移動蜱,但得到前的標籤和寫回:

f = figure(1) 
X = randi(10,10,10); 
surf(X) 
view(0,90) 

ax = gca; 
XTick = get(ax, 'XTick') 
XTickLabel = get(ax, 'XTickLabel') 
set(ax,'XTick',XTick+0.5) 
set(ax,'XTickLabel',XTickLabel) 

YTick = get(ax, 'YTick') 
YTickLabel = get(ax, 'YTickLabel') 
set(ax,'YTick',YTick+0.5) 
set(ax,'YTickLabel',YTickLabel) 

enter image description here


或者,如果你以前知道的一切,從頭開始手動操作:

[N,M] = size(X) 

set(ax,'XTick',0.5+1:N) 
set(ax,'XTickLabel',1:N) 
set(ax,'YTick',0.5+1:M) 
set(ax,'YTickLabel',1:M) 
+0

完美的答案。謝謝! –

0

標記的答案適用於衝浪或網格圖,但是,我需要一個適用於二維圖的解決方案。 這可以通過創建兩個軸來完成,一個顯示網格,另一個如下

xlabels=1:1:10;        %define where we want to see the labels 
xgrid=0.5:1:10.5;        %define where we want to see the grid 

plot(xlabels,xlabels.^2);      %plot a parabola as an example 
set(gca,'xlim',[min(xgrid) max(xgrid)]);  %set axis limits so we can see all the grid lines 
set(gca,'XTickLabel',xlabels);    %print the labels on this axis 

axis2=copyobj(gca,gcf);      %make an identical copy of the current axis and add it to the current figure 
set(axis2,'Color','none');     %make the new axis transparent so we can see the plot 
set(axis2,'xtick',xgrid,'XTickLabel','');  %set the tick marks to the grid, turning off labels 
grid(axis2,'on');        %turn on the grid 

此腳本顯示在下圖中顯示的標籤:

enter image description here