• 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])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    若想减小空白区域请在截断前定义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])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    开关box框及grid网格会有不同绘制效果:

    截断y轴:

    truncAxis('Y',[0,0.5])
    
    • 1

    另,利用一些特性可以从左到右,从下到上多次截断轴,但不推荐:

    truncAxis('X',[15,45])
    truncAxis('X',[50,55])
    
    • 1
    • 2


    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])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10


    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])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7


    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9


    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
  • 相关阅读:
    rabbitMQ
    1、SpringCloud大型企业分布式微服务云架构之Markdown 教程
    windows下OpenNN的编译与使用
    力扣--三数之和
    【精讲】vue框架 过滤器(全局及局部)、 自定义指令(内含注释)
    【虚幻引擎UE】UE4/UE5 通用插件推荐及使用介绍
    JAVA基于J2ME的手机游戏开发免费
    python自己造轮子使用
    django开发简易流程
    零信任安全:SPIFFE 和 SPIRE 通用身份验证的标准和实现
  • 原文地址:https://blog.csdn.net/slandarer/article/details/126319506