ColorOrder 设置无效

ColorOrder setting has no effect

我使用的是 Matlab R2014a 版本,我正在尝试让 plot 看起来像 Simulink 范围。我的代码正常工作,除了 ColorOrder 设置未反映在输出中。

设置 ColorOrder 后,我用 current_co=get(gca, 'ColorOrder'); 检索了它,它返回了我设置的值。但是在图表中使用了默认颜色。

这是为什么?如何修复?

my_co=[1.0 1.0 0.0; 1.0 0.0 1.0; 0.0 1.0 1.0; 1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0; 1.0 1.0 1.0];
figure('Color', [0.2 0.2 0.2]);
plot(ScopeData(:,2:6));
legend('w(t)','e(t)','y(t)','x(t)','z(t)');
set(gca, 'ColorOrder', my_co);
set(gca, 'Color', 'black');
set(gca, 'XColor', 'white');
set(gca, 'YColor', 'white');
set(gca, 'XGrid', 'on');
set(gca, 'YGrid', 'on');
title('My funky title!', 'Color', 'white');
xlabel('t/[s]');

您必须在 绘制任何内容之前设置 ColorOrder 属性 。绘图对象在创建时遵循 current ColorOrder 属性 的值,并更改 ColorOrder after 它们的创建只对未来的剧情有影响。 另请注意,您需要在绘制任何内容之前调用 hold on 以防止 axes 返回默认值 ColorOrder.

my_co = [1 1 0; 1 0 1; 0 1 1; 1 0 0; 0 1 0; 0 0 1; 1 1 1];
figure('Color', [0.2 0.2 0.2]);

% Set this before plotting anything
set(gca, 'ColorOrder', my_co);
hold on

% NOW plot your data
plot(ScopeData(:,2:6));
legend('w(t)','e(t)','y(t)','x(t)','z(t)');
set(gca, 'ColorOrder', my_co);
set(gca, 'Color', 'black');
set(gca, 'XColor', 'white');
set(gca, 'YColor', 'white');
set(gca, 'XGrid', 'on');
set(gca, 'YGrid', 'on');
title('My funky title!', 'Color', 'white');
xlabel('t/[s]');

% If you want you can turn hold off now
hold off

这是有道理的,因为如果您使用自定义颜色创建绘图:

plot(data, 'Color', 'magenta')

您不希望轴在 ColorOrder 属性 更改时自动更改此手动颜色。