How to fill the stair plots with some color.

조회 수: 25 (최근 30일)
shane watson
shane watson 2019년 9월 26일
댓글: shane watson 2019년 12월 5일
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
  댓글 수: 2
Renato SL
Renato SL 2019년 9월 26일
편집: Renato SL 2019년 9월 26일
I wouldn't call it as to fill the stair plots with some color.
I think the Figure 1 was made using the plot function, while Figure 2 was made using the stem function. The "color" you see is the stems of the plotted values.
Documentation of the stem function here.
Adam Danz
Adam Danz 2019년 9월 26일
편집: Adam Danz 2019년 9월 26일
The image in "figure 2" is not really a fill. As Renato SL pointed out, it's a bunch of stem plot lines which requires a lot of data points to appear as "filled". A patch is a better approach.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 9월 26일
편집: Adam Danz 2019년 9월 26일
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
  댓글 수: 9
Adam Danz
Adam Danz 2019년 12월 5일
편집: Adam Danz 2019년 12월 5일
I just tested my suggestion with your code and it works perfectly.
x = time
y = EP
% See rest of my suggestion
If it still doesn't work you'll need to explain what's not working and I suggest doing that in a new question rather than extending this thread since the topic slightly differs (you could provide the link to the new question here).
191205 001733-MATLAB Online R2019b.png
shane watson
shane watson 2019년 12월 5일
Thanks once again, it worked. However, I also posted related question on the following link, kindly check it https://se.mathworks.com/matlabcentral/answers/494956-adding-transparency-when-filling-color-in-plots

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by