matlab - fill underneath a curve with a gradient fill -


i can fill area underneath specific part of curve follows:

x = 0:0.01:2*pi;            y1 = sin(x)+10;  y2 = repmat(5,length(y1),1); plot(x,y1); hold on;  % fill area between 1 , 2 val = [1,2]; = 1:2;     tmp = abs(x-val(i));     [~,idx(i)] = min(tmp); end id = idx(1):1:idx(2);  x2 = x(id); y1a = y1(id); y2a = y2(id); y2a = y2a';  x=[x2,fliplr(x2)]; y=[y1a,fliplr(y2a)];  fill(x,y,'b');         

enter image description here

is possible, however, use gradient fill instead of solid color?

it great, example, use jet colormap instead. possible?

method one

here need the handle of patch created fill simplest way change last line to

hpatch = fill(x,y,'b');  

then can set colour data based on y values , set face colour interpolated

cdata=get(hpatch,'ydata'); cdata=(cdata-min(cdata))/(max(cdata)-min(cdata)); %// normalise set(hpatch,'cdata',cdata,'facecolor','interp') 

the 3 lines above ydata of patch, normalise it, set colour data of patch , set shading interpolated.
jet color map default ensure jet used add line colormap('jet') after code above.

note: normalising optional automatically normalised prefer control myself if handling multiple objects.

output

enter image description here


method two

more simply, less instructive, let matlab you, can set colour based on y in call fill: fill(x,y,y)


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -