我需要用'阴影线灰色'颜色填充两行区域。我使用了以下代码:
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并同时用'阴影线灰色'颜色填充该区域。
任何帮助将不胜感激。
您遇到的问题是X3
和X4
包含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
是点数。