【问题标题】:Fill area between two horizontal lines填充两条水平线之间的区域
【发布时间】:2023-03-22 07:10:01
【问题描述】:

我在下面的链接中关注该问题的投票最多的答案,但我没有得到任何结果。 MATLAB, Filling in the area between two sets of data, lines in one figure

我想填充一条水平线 y=6 和另一条水平线 y=9 之间的区域

x=ones(1,110)                  %#initialize x array
y1=6*(x);                      %#create first curve 
y2=9*(x);                      %#create second curve
X=[x,fliplr(x)];               %#create continuous x value array for
Y=[y1,fliplr(y2)];             %#create y values for out and
fill(X,Y,'b');                 %#plot filled area

只是它不起作用!知道为什么不?

【问题讨论】:

    标签: matlab plot fill area


    【解决方案1】:

    你快到了。

    X 应该包含 x 点的索引 (1:110) 而不是 ones(110)

    X=[1:110,fliplr(1:110)];
    

    给予

    【讨论】:

      【解决方案2】:

      您的代码可以简化:

      area([1 110],[9 9],6)    % plot a line between (x1,y1) and (x2,y2), then fill down to a baseline (6)
      ylim([0 10])             % scale y axis to fit
      

      对于直线,你只需要两个点,而不是 110。

      【讨论】: