【发布时间】:2017-10-26 13:22:11
【问题描述】:
我想使用 patch 在一个命令中绘制多个未连接的行。这些线条具有我要指定的不同颜色。不幸的是,补丁在黄色到蓝色区域使用随机颜色而不是我定义的颜色 - 我该如何解决这个问题?
clear all
close all
cla;
x=[];
y=[];
CC=[];
n=10;
for i=1:n
% define start and end from i-th line
x(end+1)=i;
x(end+1)=i+1;
% add nan to seperate lines
x(end+1)=nan;
% define start and end from i-th line
y(end+1)=i;
y(end+1)=1;
% add nan to seperate lines
y(end+1)=nan;
CC(end+1,1:3)=0.1.*i;
end
%funktioniert
figure(1)
subplot(1,2,1)
h = patch(x', y', 0);
set(h,'LineWidth',2);
set(h,'cdata', CC, 'edgecolor','flat','facecolor','none')
title('wrong colors')
subplot(1,2,2)
for i=1:n
xx=[1:3];
yy=[i i i];
line(xx,yy,'Color',CC(i,:))
hold on
end
title('wanted colors')
非常感谢! smaica
【问题讨论】:
-
你为什么用
patch画线?这段代码似乎过于笨拙... -
我发现可以在一个命令中以不同颜色绘制多条线。如果您能帮助我提供其他解决方案,我将不胜感激!
-
对于寻找单线绘图的人来说,实际设置数据是非常浪费的!初始化
x和y就像x = reshape([1:n; 2:n+1; NaN(1,n)], 1, []); y = reshape([1:n; ones(1,n), NaN(1,n)], 1, [])然后使用短循环和line看起来还不错。 -
这只是一个例子。我的实际代码在更新模拟中绘制了 >100000 行,并且不会花费超过最大值。 1秒。所以循环不起作用。