2017-02-11 64 views

回答

2

,如果你有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

Undocumented Features - Color-coded 2D line plots with color data in third dimension摘錄。原作者是thewaywewalk。您可以在contributor page上找到署名詳情。信息來源根據CC BY-SA 3.0獲得許可,可在Documentation archive中找到。參考主題ID:2383和示例ID:7849.

+0

謝謝你的解決方案! –

1

以下是一種可能的方法:使用從所需顏色圖中獲取的不同顏色明確繪製線的每個線段。

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

+1

謝謝@Luis Mendo! –