加快样条曲线的着色段?

时间:2016-10-23 01:24:39

标签: matlab plot colors matlab-figure

我正在尝试使用不同的RGB值为样条曲线的色域着色。非常感谢@Suever,我有一个工作版本:

x = [0.16;0.15;0.25;0.48;0.67];
y = [0.77;0.55;0.39;0.22;0.21];
spcv = cscvn([x, y].'); % spline curve
N = size(x, 1);
figure;
hold on;
for idx = 1:N-2
    before = get(gca, 'children'); % before plotting this segment
    fnplt(spcv, spcv.breaks([idx, idx+1]), 2);
    after = get(gca, 'children'); % after plotting this segment
    new = setdiff(after, before);
    set(new, 'Color', [idx/N, 1-idx/N, 0, idx/N]); % set new segment to a specific RGBA color
end
hold off;

现在我希望加快速度。有可能吗?

1 个答案:

答案 0 :(得分:4)

本身没有明确的基准,但你可以通过
轻松地将其矢量化   一个。收集绘制的点并将它们分成“段”(例如使用buffer函数)
  湾设置子项'color'属性(感谢@Suever指出this can be done on an array of object handles directly

%% Get spline curve
x = [0.16; 0.15; 0.25; 0.48; 0.67];
y = [0.77; 0.55; 0.39; 0.22; 0.21];
spcv = cscvn ([x, y].');

%% Split into segments
pts = fnplt (spcv);   xpts = pts(1,:).';   ypts = pts(2,:).';
idx = buffer ([1 : length(xpts)]', 10, 1, 'nodelay'); % 10pt segments
lastidx=idx(:,end); lastidx(lastidx==0)=[]; idx(:,end)=[]; % correct last segment

% Plot segments
plot (xpts(idx), ypts(idx), xpts(lastidx), ypts(lastidx), 'linewidth', 10);

% Adjust colour and transparency
Children = flipud (get (gca, 'children'));
Colours  = hsv (size (Children, 1)); % generate from colourmap
Alphas   = linspace (0, 1, length (Children)).'; % for example
set (Children, {'color'}, num2cell([Colours, Alphas],2));

enter image description here

注意:正如评论部分所指出的那样(感谢@ Dev-iL ),按照您的要求将颜色设置为RGBA四元组(即,与简单的RGB三元组相反,是一个更新的(现在,undocumented)Matlab功能。这段代码,例如在2013b中不起作用。