MATLAB | 一行代码实现截断坐标轴

Posted slandarer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB | 一行代码实现截断坐标轴相关的知识,希望对你有一定的参考价值。

说是一行代码截断,实际上是你们可以用一行代码简单调用工具函数进行截断,但这个工具函数实际上有接近两百行hiahiahia,制作不易求个赞。

0 封面效果

注:以下绘图是做出添加次刻度线,轴线加粗等一系列修饰后的效果,故与后文不同,若想达到下图效果,请查看第五部分内容。

1 单轴截断

使用方法非常简单,以下举个例子,需要注意的是截断范围一定被包括在轴范围内:

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])


2 双轴截断

要截断需要同时截断,以下给出一个例子:

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])


3 其他示例

再截断个柱状图(截断处的实线是柱状图自带的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])


4 添加图例

就照常使用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


5 与prettyAxes联动

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(varargin1,'matlab.graphics.axis.Axes')
    ax=varargin1;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',varargini),Xtrunc=varargini+1;
        case strcmpi('Y',varargini),Ytrunc=varargini+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)+MATLAB | 一行代码生成箭头坐标轴,为坐标轴增添箭头

MATLAB | 一行代码生成箭头坐标轴,为坐标轴增添箭头

.NET实现大量数据导出excel,避免被截断

如何用plt在像素坐标中画图

一个matlab绘图的小问题

matlab中画图线型怎么设置