matlabmatlab画图中,有没有办法让网格线与坐标轴数值不同步显示。

2024-12-04 22:28:48
推荐回答(2个)
回答1:

  1. clear;clc

  2. xlim([0,100]);

  3. grid on

  4. set(gca,'xtick',0:100);

  5. a=get(gca,'xticklabel');

  6. b=repmat(' ',size(a));

  7. b(mod(1:size(a,1),10)==1,:)=a(mod(1:length(a),10)==1,:);

  8. set(gca,'xticklabel',b);

2020-9-5:2017版后a从char类型变成了cell类型,修改如下

  1. clear;clc

  2. xlim([0,100]);

  3. grid on

  4. set(gca,'xtick',0:100);

  5. a=get(gca,'xticklabel');

  6. b=cell(size(a));

  7. b(mod(1:size(a,1),10)==1,:)=a(mod(1:length(a),10)==1,:);

  8. set(gca,'xticklabel',b);

回答2:

改变坐标轴显示数值,可以用set(gca,'xtick',xtick,'xticklabel',xticklabel) ;gca表示Get Current Axis的Handle,xtick表示显示刻度的位置,xticklabel表示显示的内容。

Example:
>> x = -pi:0.1:pi;
>> y = sin(x);
>> plot(x,y);
>> set(gca,'XTick',-pi:pi/2:pi);
>> set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'});
>> title('sine function');
>> title('Sine Function');
>> xlabel('Radians');
>> ylabel('Function Value');