How to shade plot with 0, 1 valued signal
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Doyoung Lee
am 7 Mai 2021
Bearbeitet: Scott MacKenzie
am 7 Mai 2021
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 Kommentare
Scott MacKenzie
am 7 Mai 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.
Akzeptierte Antwort
Scott MacKenzie
am 7 Mai 2021
Bearbeitet: Scott MacKenzie
am 7 Mai 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
0 Kommentare
Weitere Antworten (1)
DGM
am 7 Mai 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.
Siehe auch
Kategorien
Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!