首页 文章

连接2D图

提问于
浏览
1

我在MATLAB中有几个2D图 . 在每个图中都有一些线(每条线是固定长度值的行向量) . 总有一条基线(黑色),其余的彩色线可能存在也可能不存在 .

plot 1

plot 2

我需要将所有这些图连接成一个图,如下所示:
concatenated plot

请注意这些仅用于表示目的,但很好地解释了我的问题 . 我无法想象如何做到这一点 . 有人有个主意吗?一个例子可能是?此外,连续连接图之间必须存在垂直间隙,如上图所示 . 有些要点需要注意:

  • y轴对于所有图都具有固定长度

  • 如果每个单独图的x轴是1:m . 然后,最终连接图的x轴为1:(n * m),其中n是要连接的各个图的数量 .

此外,由于每条彩色线条对应于特定类型的数据,如何创建其传奇?谢谢!

2 回答

  • 2

    我在这里看到两个选项:1 . 连接到相同的图并用NaN填充以获得间隙 . 2.实际上有几个图并以巧妙的方式使用 axes .

    这是选项1的示例,首先我们将创建一些假数据:

    a1=rand(1,20);
    b1=3+rand(1,20);
    c1=6+rand(1,20);
    
    a2=rand(1,20);
    b2=3+rand(1,20);
    c2=6+rand(1,20);
    
    a3=rand(1,20);
    b3=3+rand(1,20);
    c3=6+rand(1,20);
    

    这只是用NaNs填充......

    f=@(x) [ NaN(1,round(numel(x)/5)) x ];
    

    串联:

    y1=[f(a1) f(a2) f(a3)];
    y2=[f(b1) f(b2) f(b3)];
    y3=[f(c1) f(c2) f(c3)];
    

    绘制

    x=1:numel(y1);
    plot(x,y1,x,y2,x,y3);
    set(gca,'XTickLabel',[]);
    

    enter image description here

  • 2

    This is regarding the legend part of your question:

    为了使单个图例条目具有单独的,单独绘制的项目(更准确的术语是"children of the axes object"),您应该使用hggroup . 这样,绘制的对象(例如线条)被组合在一起(从技术上讲,它们成为 hggroup 的子项,而不是直接成为 axes 的子项),因此允许您同时将某些设置应用于整个组 .

    这是一个如何工作的简单示例:

    %// Without hggroup
    figure(1337); hold all;
    x = linspace(-pi/2,pi/2,200);
    for ind=1:3
        plot(x,sin(ind*x+ind),'DisplayName',...
             ['sin(' num2str(ind) 'x+' num2str(ind) ')']);
    end
    legend('-DynamicLegend','Location','NorthWest');
    

    结果是:

    Plot with a dynamic legend without hggroup

    鉴于:

    %// With hggroup
    figure(1338); hold all;
    x = linspace(-pi/2,pi/2,200);
    prePlot=length(get(gca,'Children'));
    for ind=1:3
        plot(x,sin(ind*x+ind),'DisplayName',...
             ['sin(' num2str(ind) 'x+' num2str(ind) ')']);
    end
    postPlot=length(get(gca,'Children'));
    meshGrp = LegendGroupLatest(gca,postPlot-prePlot);
    set(meshGrp,'DisplayName','Some sines');
    legend('-DynamicLegend','Location','NorthWest');
    

    LegendGroupLatest 的位置是:

    function grpName=LegendGroupLatest(ax_handle,howMany)    
        grpName=hggroup; 
        tmp=get(ax_handle,'Children'); set(tmp(2:howMany+1),'Parent',grpName);
        set(get(get(grpName,'Annotation'),...
                    'LegendInformation'),'IconDisplayStyle','on');
    

    结果是:

    Plot with a dynamic legend including hggroup

    在此示例中,在循环内绘制的所有行都会添加到单个 hggroup 而不会影响以前绘制的项目,您显然可以添加不同的逻辑来将绘图分配给组 .

    请注意,动态图例通常会添加图表中存在的任何 line (如果您在具有动态图例的轴上绘制缩放框 - 缩放框边框会临时添加到图例中!),但 hggroup 会阻止它 .

相关问题