MATLAB: How to fill the stair plots with some color.

fillgraphMATLABpatchstaircasestairs

Hello,
Below are plots (subplot) and having Figure 1 I wanted to fill these stairs (inside) same as figure 2 . I really appreciate your help in this regard.
Figure: 1
Figure 2

Best Answer

This solution reproduces the (x,y) coordinates for the staircase and then uses fill() to fill in the area under the stairs.
% Create demo plot
X = linspace(0,4*pi,40);
Y = sin(X);
figure
sh = stairs(Y);
hold on
% Get the (y,x) coordinates from the original inputs or from
% the xdata and ydata properties of the stair object which will
% always be row vectors.
bottom = -1.5; %identify bottom; or use min(sh.YData)
x = [sh.XData(1),repelem(sh.XData(2:end),2)];
y = [repelem(sh.YData(1:end-1),2),sh.YData(end)];
% plot(x,y,'y:') %should match stair plot
% Fill area
fill([x,fliplr(x)],[y,bottom*ones(size(y))], 'g')
190926 064128-Figure 1.png