在两条用户定义的线之间和下面的预先存在的数据之间绘制阴影区域

时间:2013-11-30 18:56:15

标签: matlab plot area

我正在尝试在两个用户定义的垂直线之间添加灰色阴影区域。我需要这个灰色阴影区域显示在我已绘制的数据下方。我已经尝试使用填充和区域功能,但是无法成功创建出现在Matlab图下方的有界区域。我需要阴影区域图从x轴在5.5和19处创建的垂直线延伸,并在900(左侧y轴)和1(右侧y轴)处向上延伸到y轴。见这里:https://www.dropbox.com/s/qyzkuhw17yxn8p5/sample.png

3 个答案:

答案 0 :(得分:2)

我认为问题不在于生成阴影区域,而在于使其在下方其他元素中出现,即使它后来被绘制。您可以通过将其定位为“更深”,即以更小的z值来实现。这是一个例子,基于randomatlabuser的代码构建:

x = linspace(0, 24, 100);
plot(x, 450 - 400 * cos(x / 12 * pi), 'k.-')
hold all

x = [5.5 5.5 19 19 5.5];
y = [0 900 900 0 0];
patch(x, y, -1 * ones(size(x)), [0.9 0.9 0.9], 'LineStyle', 'none')

patch与fill基本相同,但有一个支持第三个坐标的版本,即z值。

答案 1 :(得分:1)

根据我的理解,您想要这样做:

h1 = plot((0:24), (0:700/24:700), '-b', 'LineWidth', 10); % some function
hold on
h2 = plot((0:24), 1.5 * (0:24) .^2, '-r', 'LineWidth', 10); % some other function

x = [5.5 5.5 19 19 5.5]; y = [0 900 900 0 0]; % define edges of area
h3 = fill(x, y, [0.9 0.9 0.9], 'LineStyle', 'none'); % fill area

set(gca, 'Children', [h1 h2 h3]) % h3 in the background, then h2 and finally h1 upfront

如果没有,你需要更好地解释你的目标。

答案 2 :(得分:0)

    % Add shaded area to plot

    % Line 1
    hl1 = line(x1,y1,'Color','b'); 

    x = [5.5 5.5 19 19]; y = [0 900 900 0]; % define edges of shaded area

    % Locations for 5:30am and 19:00pm with the left y-axis ranging from 0-900 

    hl2=fill([x(1) x(2) x(3) x(4)], [y(1) y(2) y(3) y(4)],'Color','r','EdgeColor','none','LineStyle','none'); 

    hl1 = line(x1,y1,'Color','b'); % Replot line 1 over shaded area
    hold on; 

    % Continue adding more shaded areas using the fill function or add lines directly...