How can I fill area between two curves with custom colors?

19 views (last 30 days)
I am trying to shade the area between two curves ('layers') on a plot, where the color of the shading depends on the vertical distance between the two layers.
The issue is that I can't figure out how to get the right colors to show up. I have tried using patch and fill, but they create a gradient between the vertices instead of just a flat color.
At each point along the layers I calculate the distance between the layers and then create an array of ratios that should inform the color of the shading at that point. For example, if the ratio is smaller than ~1 shade the area green, if the ratio is greater than ~1 shade it blue, and if it is about 1 then leave it white. (The point of this is to see where the layers are being compressed or stretched vertically).
close all;
L32=Layer32(2660:3075); %just trimming the raw data
L33=Layer33(2660:3075);
d32=d(2660:3075);
diff32=L32-L33; %vertical difference between the two layers
refdiff32=L32(1)-L33(1); %creating the 'reference' difference
ratio32=abs(diff32/refdiff32); %array of ratios
%making my color array from the ratio values
color32=zeros(size(ratio32));
for i=1:1:length(color32)
if ratio32(i)<0.9 %layers squish together
color32(i)=0;
elseif ratio32(i)>1.1 %layers stretch apart
color32(i)=3;
else
color32(i)=6; %layers are abt parallel
end
end
%making the color array the same size as the other inputs, probably not an elegant way to do it.
color32=[color32;color32];
%Not sure if I need to transpose color32, but doesn't seem to make a difference either way.
patch([d32' fliplr(d32')], [L32' fliplr(L33')], color32');
The numbers that the for loop puts into the array are kind of arbitrary, I have also tried 'r', 'g', etc. Here is what I'm getting out of the above code:
I have read the help pages for patch and fill but am still stuck. Any advice would be appreciated!

Accepted Answer

Matt J
Matt J on 5 Aug 2021
Edited: Matt J on 5 Aug 2021
Maybe as below:
N=500;
x=linspace(0,pi,N);
y1=cos(x)-0.3;
y2=cos(x-pi/6);
ratio32= normalize( abs( (y2-y1)./(y2(1)-y1(1)) ) ,'range');
color32=floor(ratio32*255+1);
xp=[x(1:end-1);x(2:end)]; xp=[xp;flipud(xp)];
yp=[y1(1:end-1);y1(2:end); y2(1:end-1);y2(2:end)];
set(plot(x,y1,'k',x,y2,'k'),'LineWidth',3);
P=patch(xp,yp,color32(1:end-1),'EdgeColor','none');
  3 Comments
Matt J
Matt J on 5 Aug 2021
Edited: Matt J on 5 Aug 2021
L32 is 1x416, color32 is 1x416, and d32 is 416x1
Well, there you are. d32 is not the same size as the others. It should be a row vector.
Georgia Carroll
Georgia Carroll on 5 Aug 2021
Edited: Georgia Carroll on 5 Aug 2021
adding this line does not change the error
d32=d32';
also I don't think it should matter anyway, length(d32)=length(d32') ?
Edit - nevermind! I just made myself realize I should have been looking at length instead of size and I found that I said 'end' instead of 'end-1' on my color32. This is my result.
Thank you so much! This was so helpful.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!