MATLAB画图中,有时候需要对y轴某个区间的刻度放大,以突显值的细微区别。下面以sigmod函数为例,进行实际操作:
x = -5:0.01:5;
y1=1./(1+exp(-x));
y2=1./(1+exp(-2*x));
figure
plot(x,y1,x,y2)
xlabel('x')
ylabel('y')
legend('y1','y2')
title('原图')
grid on
up=1;%y1与y2目标最大值
low=0;%y1与y2目标最小值
%% 将y轴在[m,up]进行放大
figure
m = 0.9;%y轴放大区间[m,up],即在[0.9,1]区间进行放大
s = 10;%将放大区间内的数乘以放大倍数s
Y1 = y1-m;%向下平移
Y2 = y2-m;%向下平移
Y1(Y1>0) = Y1(Y1>0)*s;%扩倍放大
Y2(Y2>0) = Y2(Y2>0)*s;%扩倍放大
Y1 = Y1 + m;%向上平移
Y2 = Y2 + m;%向上平移
h = plot(x,Y1,x,Y2);
set(gca,'Ytick',[low,0.2,0.4,0.6,0.8,m,s*(up-m)+m]); ylim([low,s*(up-m)+m])
set(gca,'Yticklabel',[low,0.2,0.4,0.6,0.8,m,up])
grid on
xlabel('x')
ylabel('y')
legend('y1','y2')
title('对y轴区间[0.9,1]放大')
%% 将y轴在[low,m]进行放大
figure
m = 0.1;%y轴放大区间[low,m],即在[0,0.1]区间进行放大
s = 10;%将放大区间内的数乘以放大倍数s
Y1 = y1-m;%向下平移
Y2 = y2-m;%向下平移
Y1(Y1<0) = Y1(Y1<0)*s;%扩倍放大
Y2(Y2<0) = Y2(Y2<0)*s;%扩倍放大
Y1 = Y1 + m;%向上平移
Y2 = Y2 + m;%向上平移
h = plot(x,Y1,x,Y2);
set(gca,'Ytick',[-m*s+m,m,0.2,0.4,0.6,0.8,up]); ylim([-m*s+m,up])
set(gca,'Yticklabel',[low,m,0.2,0.4,0.6,0.8,up])
grid on
xlabel('x')
ylabel('y')
legend('y1','y2')
title('对y轴区间[0,0.1]放大')