Fill in the region between two line curves.
3 views (last 30 days)
Show older comments
Hi,
I need to fill the region of two lines by 'hatched grey' color. I used the following code:
M = [0.54, 0.7, 0.74, 0.78];
X1 = [0.007856, 0.008394, 0.008607, 0.012584]; %X1 values
X2 = [0.007901, 0.008461, 0.008739, 0.011302]; %X2 values
xq = (0.54:0.000001:0.8); %adding intervals to make line smoother
X3 = interpn(M,X1,xq,'pchip'); %other type: pchip, makima, spline etc.
X4 = interpn(M,X2,xq,'pchip'); %other type: pchip, makima, spline etc.
set(0,'defaulttextinterpreter','latex')
figure(1)
hold all;
plot(xq, X3,'r-','LineWidth',1.5);
plot(xq, X4,'b--','LineWidth',1.5);
X=[xq,fliplr(xq)]; %create continuous x value array for plotting
Y=[X3,fliplr(X4)]; %create y values for out and then back
fill(X,Y,'color',[17 17 17]); %plot filled area
xlabel('X','FontSize',12); % Label the x axis
ylabel('Y','FontSize',12); % Label the y axis
axis([0.5 0.8 0.007 0.013]);
However, when I tried to make the line smoother, the fill in command didn't take place! I need to keep the line smoother and fill the region with a 'hatched grey' color at the same time.
Any help will be appreciated.
2 Comments
Walter Roberson
on 14 Jun 2020
Your lines cross. You need to find the interesection and plot only within the shared area.
[~, idx] = min(abs(X3-X4));
Now use only 1:idx as the portion to fill inside.
Answers (1)
Image Analyst
on 14 Jun 2020
"Any help will be appreciated." <== See the FAQ: https://matlab.fandom.com/wiki/FAQ#How_do_I_shade_the_region_between_two_curves.3F
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!