Fill in the region between two line curves.

조회 수: 5 (최근 30일)
Rahman
Rahman 2020년 6월 14일
댓글: Rahman 2020년 6월 14일
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
Walter Roberson
Walter Roberson 2020년 6월 14일
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.
Rahman
Rahman 2020년 6월 14일
Thank you.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2020년 6월 14일

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by