ColorOrder setting has no effect

99封情书 提交于 2019-12-02 09:57:34

问题


I am using Matlab version R2014a and I am trying to have plot look like the Simulink scope. My code works as it should except, the ColorOrder setting is not reflected in the output.

Right after setting ColorOrder I retrieved it with current_co=get(gca, 'ColorOrder'); and it gives back the value that I have set. However in the diagram the default colors are used.

Why is this? How can it be fixed?

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]');

回答1:


You have to set the ColorOrder property before plotting anything. Plot objects respect the current value of the ColorOrder property when they are created and changing the ColorOrder after they are created only has an effect on future plots. Also note that you need to call hold on prior to plotting anything to prevent the axes from going back to the default 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

This makes sense because if you create a plot using a custom color:

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

You wouldn't want the axes automatically changing this manual color when the ColorOrder property is changed.



来源:https://stackoverflow.com/questions/37809245/colororder-setting-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!