Draw rectangle on existing graph
245 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
deejt
am 4 Jun. 2021
Kommentiert: Star Strider
am 10 Mär. 2023
How can I plot a rectangle over an existing graph using vectors and matrices, instead coordinates?
I plotted a graph from a matrix.
a = rand([-3.5,1.5],1300,3) % matrix to plot with each row representing 1 line in graph
plot(times,a) % times is the vector with the time points, x axis
ylim([1.5 -3.5]) % I prepare the axis of y so that negative is on top, and positive at the bottom
set(gca,'YDir','reverse') % I need to plot the negative as upward going, therefore I reverse the axis
hold on;
Now I have two vectors and would like to plot the vectors as two rectangles over the existing graph. However, I found some information about having to rezize the graph before I can plot the rectangles and in the documentation I could not find the information to use a vector. Instead the documentation requires that the coordinates are typed in.
rect_1idx = [66,166] % the first rectangle should start at 66 on the x axis and end on 166 on the x axis
rect_2idx = [170,270] % the same as above
% the hight of the rectangle should go from -3.5 to 1.5 on the y axis
This is what I have tried and it did not work out
rectangle('Position',[-3.5 66 100 1.5])
The goal is to get a figure similar to this one https://www.researchgate.net/publication/330810145/figure/fig3/AS:721682620772352@1549073953701/ERP-wave-from-O2-ERP-waveform-from-electrode-O2-displaying-the-P1-component-Cannabis_W640.jpg
Do you guys have an idea how I could solve this?
Thank you for you help!
2 Kommentare
Adam Danz
am 4 Jun. 2021
This is not a valid syntax: a = rand([-3.5,1.5],1300,3).
Perhaps you meant,
bounds = [-3.5,1.5];
a = rand(1300,3)*range(bounds)+bounds(1);
This is also no valid: ylim([1.5 -3.5]) since the bounds must be in ascending order.
ylim(bounds)
The rectangle you drew starts at x=-3.5 and y=66; you probably meant x=66 and y=-3.5
rectangle('Position',[66 -3.5 100 1.5])
Finally, if you want curvature like the rectangles shown in the link you shared,
rectangle('Position',[66 -3.5 100 1.5],'Curvature',0.3)
Akzeptierte Antwort
Star Strider
am 4 Jun. 2021
Create an anonymous function ‘rectplot’ (name it whatever you want) to do the calculations and plotting —
x = linspace(0, 300, 500);
y = 0.5*sin(2*pi*x.^0.35)-3;
rectplot = @(x1,x2) rectangle('Position',[x1 -3.5 (x2-x1) 1.5]);
figure
plot(x, y)
ylim([-5 -1])
rectplot(66,166)
rectplot(170,270)
.
8 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations 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!