说是一行代码截断,实际上是你们可以用一行代码简单调用工具函数进行截断,但这个工具函数实际上有接近两百行hiahiahia,制作不易求个赞。
注:以下绘图是做出添加次刻度线,轴线加粗等一系列修饰后的效果,故与后文不同,若想达到下图效果,请查看第五部分内容。
使用方法非常简单,以下举个例子,需要注意的是截断范围一定被包括在轴范围内:
x=0:.05:60;
y=4.*cos(x)./(x+2);
plot(x,y,'LineWidth',2)
box on
grid on
truncAxis('X',[15,45])
若想减小空白区域请在截断前定义axes位置:
x=0:.05:60;
y=4.*cos(x)./(x+2);
plot(x,y,'LineWidth',2)
box on
grid on
set(gca,'Position',[0.06,0.06,.92,.92]);
truncAxis('X',[15,45])
开关box
框及grid
网格会有不同绘制效果:
截断y轴:
truncAxis('Y',[0,0.5])
另,利用一些特性
可以从左到右,从下到上多次截断轴,但不推荐:
truncAxis('X',[15,45])
truncAxis('X',[50,55])
要截断需要同时截断,以下给出一个例子:
X=linspace(0,2*pi,100);
Y=exp(0.3*X).*sin(3*X);
scatter(X,Y,'LineWidth',1);
box off
grid off
set(gca,'Position',[0.06,0.06,.92,.92]);
truncAxis('X',[3,4],'Y',[0,1])
再截断个柱状图(截断处的实线是柱状图自带的0基线删不掉):
y=[120 91 105 143.5 131 150 179 203 226 249 281.5,800,820,800];
bar(y)
box off
set(gca,'Position',[0.06,0.06,.92,.92]);
truncAxis('Y',[300,700])
就照常使用legend
函数即可,图例默认生成在右上角:
X=linspace(0,2*pi,100);
Y=exp(0.3*X).*sin(3*X);
scatter(X,Y,'LineWidth',1);
box off
grid off
set(gca,'Position',[0.06,0.06,.92,.92]);
truncAxis('X',[3,4],'Y',[0,1])
legend
y=[120 91 105 143.5 131 150 179 203 226 249 281.5,800,820,800];
bar(y)
box off
set(gca,'Position',[0.06,0.06,.92,.92]);
truncAxis('Y',[300,700])
legend
y=[120 91 105 143.5 131 150 179 203 226 249 281.5,800,820,800];
bar(y)
box off
set(gca,'Position',[0.06,0.06,.92,.92]);
prettyAxes().gbase()
truncAxis('Y',[300,700])
legend
prettyAxes:
函数获取(点击图片跳转链接):
function truncAxis(varargin)
% 获取参数
if isa(varargin{1},'matlab.graphics.axis.Axes')
ax=varargin{1};varargin(1)=[];
else
ax=gca;
end
hold(ax,'on');
% box(ax,'off')
ax.XAxisLocation='bottom';
ax.YAxisLocation='left';
axisPos=ax.Position;
axisXLim=ax.XLim;
axisYLim=ax.YLim;
axisXScale=diff(axisXLim);
axisYScale=diff(axisYLim);
truncRatio=1/20;
Xtrunc=[];Ytrunc=[];
for i=1:length(varargin)-1
switch true
case strcmpi('X',varargin{i}),Xtrunc=varargin{i+1};
case strcmpi('Y',varargin{i}),Ytrunc=varargin{i+1};
end
end
switch true
case isempty(Xtrunc)
% 复制坐标区域
ax2=copyAxes(ax);
% 修改轴基础属性
ax2.XTickLabels=[];
ax2.XColor='none';
% 修改坐标区域范围
ax.YLim=[axisYLim(1),Ytrunc(1)];
ax2.YLim=[Ytrunc(2),axisYLim(2)];
% 坐标区域重定位
ax.Position(4)=axisPos(4)*(1-truncRatio)/(axisYScale-diff(Ytrunc))*(Ytrunc(1)-axisYLim(1));
ax2.Position(2)=axisPos(2)+ax.Position(4)+axisPos(4)*truncRatio;
ax2.Position(4)=axisPos(4)*(1-truncRatio)/(axisYScale-diff(Ytrunc))*(axisYLim(2)-Ytrunc(2));
% 链接轴范围变动
linkaxes([ax,ax2],'X')
% 添加线和标识符
if strcmp(ax.Box,'on')
ax.Box='off';ax2.Box='off';
annotation('line',[1,1].*(ax.Position(1)+ax.Position(3)),[ax.Position(2),ax.Position(2)+ax.Position(4)],'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax.Position(1)+ax.Position(3)),[ax2.Position(2),ax2.Position(2)+ax2.Position(4)],'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax2.Position(2)+ax2.Position(4)),'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
else
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax.Position(2)+ax.Position(4)),'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax2.Position(2)),'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
end
createSlash([ax.Position(1)-.2,ax.Position(2)+ax.Position(4)-.2,.4,.4])
createSlash([ax.Position(1)-.2,ax2.Position(2)-.2,.4,.4])
createSlash([ax.Position(1)+ax.Position(3)-.2,ax.Position(2)+ax.Position(4)-.2,.4,.4])
createSlash([ax.Position(1)+ax.Position(3)-.2,ax2.Position(2)-.2,.4,.4])
case isempty(Ytrunc)
% 复制坐标区域
ax2=copyAxes(ax);
% 修改轴基础属性
ax2.YTickLabels=[];
ax2.YColor='none';
% 修改坐标区域范围
ax.XLim=[axisXLim(1),Xtrunc(1)];
ax2.XLim=[Xtrunc(2),axisXLim(2)];
% 坐标区域重定位
ax.Position(3)=axisPos(3)*(1-truncRatio)/(axisXScale-diff(Xtrunc))*(Xtrunc(1)-axisXLim(1));
ax2.Position(1)=axisPos(1)+ax.Position(3)+axisPos(3)*truncRatio;
ax2.Position(3)=axisPos(3)*(1-truncRatio)/(axisXScale-diff(Xtrunc))*(axisXLim(2)-Xtrunc(2));
% 链接轴范围变动
linkaxes([ax,ax2],'y')
% 添加线和标识符
if strcmp(ax.Box,'on')
ax.Box='off';ax2.Box='off';
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax.Position(2)+ax.Position(4)),'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax2.Position(1),ax2.Position(1)+ax2.Position(3)],[1,1].*(ax.Position(2)+ax.Position(4)),'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax2.Position(1)+ax2.Position(3)),[ax2.Position(2),ax2.Position(2)+ax2.Position(4)],'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
else
annotation('line',[1,1].*(ax.Position(1)+ax.Position(3)),[ax2.Position(2),ax2.Position(2)+ax2.Position(4)],'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax2.Position(1)),[ax2.Position(2),ax2.Position(2)+ax2.Position(4)],'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
end
createSlash([ax.Position(1)+ax.Position(3)-.2,ax.Position(2)-.2,.4,.4])
createSlash([ax2.Position(1)-.2,ax.Position(2)-.2,.4,.4])
createSlash([ax.Position(1)+ax.Position(3)-.2,ax.Position(2)+ax.Position(4)-.2,.4,.4])
createSlash([ax2.Position(1)-.2,ax.Position(2)+ax.Position(4)-.2,.4,.4])
case (~isempty(Ytrunc))&(~isempty(Ytrunc))
% 复制坐标区域
ax2=copyAxes(ax);
ax3=copyAxes(ax);
ax4=copyAxes(ax);
% 修改轴基础属性
ax2.XTickLabels=[];
ax2.XColor='none';
ax3.XTickLabels=[];
ax3.XColor='none';
ax3.YTickLabels=[];
ax3.YColor='none';
ax4.YTickLabels=[];
ax4.YColor='none';
% 修改坐标区域范围
ax.YLim=[axisYLim(1),Ytrunc(1)];
ax.XLim=[axisXLim(1),Xtrunc(1)];
ax2.XLim=[axisXLim(1),Xtrunc(1)];
ax2.YLim=[Ytrunc(2),axisYLim(2)];
ax3.XLim=[Xtrunc(2),axisXLim(2)];
ax3.YLim=[Ytrunc(2),axisYLim(2)];
ax4.XLim=[Xtrunc(2),axisXLim(2)];
ax4.YLim=[axisYLim(1),Ytrunc(1)];
% 坐标区域重定位
ax.Position(3)=axisPos(3)*(1-truncRatio)/(axisXScale-diff(Xtrunc))*(Xtrunc(1)-axisXLim(1));
ax.Position(4)=axisPos(4)*(1-truncRatio)/(axisYScale-diff(Ytrunc))*(Ytrunc(1)-axisYLim(1));
ax2.Position(2)=axisPos(2)+ax.Position(4)+axisPos(4)*truncRatio;
ax2.Position(3)=axisPos(3)*(1-truncRatio)/(axisXScale-diff(Xtrunc))*(Xtrunc(1)-axisXLim(1));
ax2.Position(4)=axisPos(4)*(1-truncRatio)/(axisYScale-diff(Ytrunc))*(axisYLim(2)-Ytrunc(2));
ax3.Position(1)=axisPos(1)+ax.Position(3)+axisPos(3)*truncRatio;
ax3.Position(2)=axisPos(2)+ax.Position(4)+axisPos(4)*truncRatio;
ax3.Position(3)=axisPos(3)*(1-truncRatio)/(axisXScale-diff(Xtrunc))*(axisXLim(2)-Xtrunc(2));
ax3.Position(4)=axisPos(4)*(1-truncRatio)/(axisYScale-diff(Ytrunc))*(axisYLim(2)-Ytrunc(2));
ax4.Position(1)=axisPos(1)+ax.Position(3)+axisPos(3)*truncRatio;
ax4.Position(3)=axisPos(3)*(1-truncRatio)/(axisXScale-diff(Xtrunc))*(axisXLim(2)-Xtrunc(2));
ax4.Position(4)=axisPos(4)*(1-truncRatio)/(axisYScale-diff(Ytrunc))*(Ytrunc(1)-axisYLim(1));
% 链接轴范围变动
linkaxes([ax3,ax2],'y')
linkaxes([ax4,ax3],'x')
linkaxes([ax,ax2],'x')
linkaxes([ax,ax4],'y')
% 添加线和标识符
if strcmp(ax.Box,'on')
ax.Box='off';ax2.Box='off';ax3.Box='off';ax4.Box='off';
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax2.Position(2)+ax2.Position(4)),'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax3.Position(1),ax3.Position(1)+ax3.Position(3)],[1,1].*(ax2.Position(2)+ax2.Position(4)),'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax4.Position(1)+ax4.Position(3)),[ax3.Position(2),ax3.Position(2)+ax3.Position(4)],'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax4.Position(1)+ax4.Position(3)),[ax4.Position(2),ax4.Position(2)+ax4.Position(4)],'LineStyle','-','LineWidth',ax.LineWidth,'Color',ax.XColor);
else
annotation('line',[1,1].*(ax.Position(1)+ax.Position(3)),[ax2.Position(2),ax2.Position(2)+ax2.Position(4)],'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax3.Position(1)),[ax2.Position(2),ax2.Position(2)+ax2.Position(4)],'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax.Position(1)+ax.Position(3)),[ax.Position(2),ax.Position(2)+ax.Position(4)],'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[1,1].*(ax3.Position(1)),[ax.Position(2),ax.Position(2)+ax.Position(4)],'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax.Position(2)+ax.Position(4)),'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax.Position(1),ax.Position(1)+ax.Position(3)],[1,1].*(ax2.Position(2)),'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax4.Position(1),ax4.Position(1)+ax4.Position(3)],[1,1].*(ax.Position(2)+ax.Position(4)),'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
annotation('line',[ax4.Position(1),ax4.Position(1)+ax4.Position(3)],[1,1].*(ax2.Position(2)),'LineStyle',':','LineWidth',ax.LineWidth,'Color',ax.XColor);
end
createSlash([ax.Position(1)-.2,ax.Position(2)+ax.Position(4)-.2,.4,.4])
createSlash([ax.Position(1)-.2,ax2.Position(2)-.2,.4,.4])
createSlash([ax4.Position(1)+ax4.Position(3)-.2,ax.Position(2)+ax.Position(4)-.2,.4,.4])
createSlash([ax4.Position(1)+ax4.Position(3)-.2,ax2.Position(2)-.2,.4,.4])
createSlash([ax.Position(1)+ax.Position(3)-.2,ax.Position(2)-.2,.4,.4])
createSlash([ax.Position(1)+ax.Position(3)-.2,ax2.Position(2)+ax2.Position(4)-.2,.4,.4])
createSlash([ax4.Position(1)-.2,ax.Position(2)-.2,.4,.4])
createSlash([ax4.Position(1)-.2,ax2.Position(2)+ax2.Position(4)-.2,.4,.4])
% 修改当前坐标区域,方便legend添加
set(gcf,'currentAxes',ax3)
end
% 复制原坐标区域全部可复制属性
function newAX=copyAxes(ax)
axStruct=get(ax);
fNames=fieldnames(axStruct);
newAX=axes('Parent',ax.Parent);
coeList={'CurrentPoint','XAxis','YAxis','ZAxis','BeingDeleted',...
'TightInset','NextSeriesIndex','Children','Type','Legend'};
for n=1:length(coeList)
coePos=strcmp(fNames,coeList{n});
fNames(coePos)=[];
end
for n=1:length(fNames)
newAX.(fNames{n})=ax.(fNames{n});
end
copyobj(ax.Children,newAX)
end
% 添加截断标识符函数
function createSlash(pos)
anno=annotation('textbox');
anno.String='/';
anno.LineStyle='none';
anno.FontSize=15;
anno.Position=pos;
anno.FitBoxToText='on';
anno.VerticalAlignment='middle';
anno.HorizontalAlignment='center';
end
end