首页 文章

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

提问于
浏览
2

在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 回答

  • 2

    你的 fig 是一个轴的句柄,而不是图:

    fig = subplot(2,1,1);
    

    但是,当您设置 'defaultAxesColorOrder' 属性时,将其设置为其中所有轴的图形级别,如the docs所示:

    在图形级别设置默认值,以便新颜色仅影响图中子项的轴 .

    你需要做的就是将 fig 定义为数字,并在设置 'defaultAxesColorOrder' 属性后移动subplot命令:

    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

相关问题