Fill in the region between two line curves.

3 次查看(过去 30 天)
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.

请先登录,再进行评论。

回答(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