我试图将MATLAB的刻度线与我的网格对齐,但我无法找到偏移标签的好方法。
另外,如果我运行set(gca,'XTickLabel',1:10)
,我的x刻度标签最终会从1到5不等。给出了什么?
答案 0 :(得分:0)
标记的答案适用于冲浪或网格图,但是,我需要一个适用于2D图的解决方案。 这可以通过创建两个轴来完成,一个用于显示网格,另一个用于显示标签,如下所示
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
此脚本显示下图: