How to shade plot with 0, 1 valued signal

3 views (last 30 days)
It's hard for me to how to describe the problem, but let me try.
For now, I did some animal experiment and want to plot two different data in one figure.
The first one has a continuous value and I can use plot. But the second has discrete value of 0 and 1, and I want to change the background color according to the value.
At first, I tried using area function and following is the simplified code and result.
figure();
plot(first_data.t, first_data.y);
hold on
area(second_data.t, [0, second_data.y], 'FaceColor', 'r', 'FaceAlpha', 0.3, 'LineStyle', 'none')
I found that the area function is not working as I thought via the second figure.
The second data indicates the state of the time interval, for example, second_data.y(1) indicates the value between t(1) and t(2). So I want a rectangular shaped area instead of the triangular area as shown in the second figure.
I think if I can draw a line which rise and fall vertically like step functions but I cannot find how to...
I would be really appreciate if someone shares the idea
  2 Comments
Scott MacKenzie
Scott MacKenzie on 7 May 2021
I don't quite understand. The first figure above is what you want, correct? And your code generated that figure, so I'm not sure what the issue is.
Doyoung Lee
Doyoung Lee on 7 May 2021
Thanks for the reply. I think I missed some point. And maybe the phrases 'first' and 'second' are confusing as there are two data, and two figures with same data.
The second gifure is an enlargement of the first picture. It seems there is no problem, but when the value of second data changed rapidly, it was not able to clearly fill the area.

Sign in to comment.

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 7 May 2021
Edited: Scott MacKenzie on 7 May 2021
Let me add this as a possible solution. The trick, as always, is to get the right data and organize the data in some convenient manner. My assumption below is that you have, or can easily obtain, the x-coordinates for the start and end of each rectangle.
y = rand(1, 1000) + 2.5;
plot(y);
ax = gca;
ax.YLim = [0 7];
% each row contains x coordinates for start and end of rectangle
a = [25 50; 100 110; 300 350; 700 800; 900 920];
y = ax.YLim(1);
h = ax.XLim(2) - ax.XLim(1);
for i=1:size(a,1)
x = a(i,1);
w = a(i,2) - a(i,1);
rectangle('position', [x y w h], 'facecolor', [1 0 0 .3], 'edgecolor', 'none');
end

More Answers (1)

DGM
DGM on 7 May 2021
Something like this:
x = [0 1 1 2 2 3 4 4 5 5 6];
y = [0 0 1 1 0 0 0 1 1 0 0];
area(x,y,'facecolor','m','facealpha',0.2,'linestyle','none')
You can make lines vertical if you want.
  1 Comment
Doyoung Lee
Doyoung Lee on 7 May 2021
Is this work as two points exist in the same x value? If then, I should modify my data but it seems worth. Thanks!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!