代码之家  ›  专栏  ›  技术社区  ›  phdstudent

在子图中设置两个y轴颜色

  •  2
  • phdstudent  · 技术社区  · 8 年前

    在matlab文档中,它表示可以通过以下操作更改双y轴图形中的matlab轴颜色:

    fig = figure;
    left_color = [.5 .5 0];
    right_color = [0 .5 .5];
    set(fig,'defaultAxesColorOrder',[left_color; right_color]);
    
    y = [1 2 3; 4 5 6];
    yyaxis left
    plot(y)
    
    z = [6 5 4; 3 2 1];
    yyaxis right
    plot(z)
    

    这可以工作并产生所需的输出。

    enter image description here

    现在我试着做完全相同的数字,但在一个子图中。我的代码如下:

    fig = subplot(2,1,1);
    left_color = [.5 .5 0];
    right_color = [0 .5 .5];
    set(fig,'defaultAxesColorOrder',[left_color; right_color]);
    
    y = [1 2 3; 4 5 6];
    yyaxis left
    plot(y)
    
    z = [6 5 4; 3 2 1];
    yyaxis right
    plot(z)
    

    enter image description here

    然而,在这里,它不会改变轴的颜色。你有什么想法吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   EBH    8 年前

    你的 fig 是轴的手柄,而不是图形:

    fig = subplot(2,1,1);
    

    但是,当您设置 'defaultAxesColorOrder' the docs :

    在地物级别设置默认值,以便新颜色仅影响作为地物子对象的轴 无花果 .

    你需要做的就是定义 无花果 如图所示,并在设置 'DefaultAxesColorder' 属性:

    fig = figure;  %<-- you change here
    left_color = [.5 .5 0];
    right_color = [0 .5 .5];
    set(fig,'defaultAxesColorOrder',[left_color; right_color]);
    
    subplot(2,1,1) %<-- and add that
    y = [1 2 3; 4 5 6];
    yyaxis left
    plot(y)
    
    z = [6 5 4; 3 2 1];
    yyaxis right
    plot(z)
    

    enter image description here

    推荐文章