ColorOrder设置无效 [英] ColorOrder setting has no effect
问题描述
我正在使用Matlab版本R2014a,并且试图使plot
看起来像Simulink范围.我的代码可以正常工作,除了ColorOrder
设置未反映在输出中.
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.
设置ColorOrder
之后,我立即用current_co=get(gca, 'ColorOrder');
检索了它,并返回了我设置的值.但是,在图中使用了默认颜色.
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]');
推荐答案
在绘制任何东西之前,必须设置ColorOrder
属性 .绘图对象在创建时会尊重ColorOrder
属性的 current 值,并且在创建后更改ColorOrder
只会影响将来的绘图. 还请注意,您在绘制任何内容之前需要先调用hold on
,以防止axes
返回默认的ColorOrder
.
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')
当更改ColorOrder
属性时,您不希望轴自动更改此手动颜色.
You wouldn't want the axes automatically changing this manual color when the ColorOrder
property is changed.
这篇关于ColorOrder设置无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!