填充MATLAB中两条直线之间的区域

问题描述 投票:0回答:1

我需要用'阴影线灰色'颜色填充两行区域。我使用了以下代码:

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,'g');    %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]);

但是,当我试图使线条更平滑时,填充命令没有发生!我需要保留line smoother并同时用'阴影线灰色'颜色填充该区域。

任何帮助将不胜感激。

matlab matlab-figure
1个回答
0
投票

您遇到的问题是X3X4包含NaN值。在您尝试推断的情况下会发生这种情况,interpn在此处添加NaN值。

数组xq以0.8结束,而输入数据M以0.78结束。您也应该使xq的结尾为0.78。最好的方法如下:

xq = M(1):0.000001:M(end);

尽管要注意,间隔非常小,这导致要绘制的数据点过多,但对于平滑的绘制,不必有这么多的点。我建议您改用linspace

xq = linspace(M(1), M(end), 1000);

[这里,1000是点数。

© www.soinside.com 2019 - 2024. All rights reserved.