Matlab中的2-D线条渐变色

8

在Matlab中,是否有可能为2-D线条添加渐变色,特别是当您只有少量数据点(少于10个)时,结果类似于下面图片中的效果?

enter image description here

2个回答

9
这不难,如果你有MATLAB R2014b或者更高版本。
n = 100;
x = linspace(-10,10,n); y = x.^2;
p = plot(x,y,'r', 'LineWidth',5);

% modified jet-colormap
cd = [uint8(jet(n)*255) uint8(ones(n,1))].';

drawnow
set(p.Edge, 'ColorBinding','interpolated', 'ColorData',cd)

这将导致:

enter image description here

节选自未记录的功能 - 具有第三维颜色数据的彩色二维线图。原作者是thewaywewalk。归属详细信息可在贡献者页面上找到。源代码根据CC BY-SA 3.0许可,并可在文档存档中找到。参考主题ID:2383,示例ID:7849。

感谢您提供的解决方案! - Claude Monet

5
这里有一个可能的方法:使用所需色图中不同的颜色显式绘制线条的每个部分。
x = 1:10; % x data. Assumed to be increasing
y = x.^2; % y data
N = 100; % number of colors. Assumed to be greater than size of x
cmap = parula(N); % colormap, with N colors
linewidth = 1.5; % desired linewidth
xi = x(1)+linspace(0,1,N+1)*x(end); % interpolated x values
yi = interp1(x,y,xi); % interpolated y values
hold on
for n = 1:N
    plot(xi([n n+1]), yi([n n+1]), 'color', cmap(n,:), 'linewidth', linewidth);
end

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接