ptmartin
ptmartin

Reputation: 45

ColorOrder setting has no effect

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

Upvotes: 1

Views: 620

Answers (1)

Suever
Suever

Reputation: 65430

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.

enter image description here

Upvotes: 1

Related Questions