Draw rectangle on existing graph

275 次查看(过去 30 天)
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])
Do you guys have an idea how I could solve this?
Thank you for you help!
  2 个评论
Adam Danz
Adam Danz 2021-6-4
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)
All of this is explained in the documentation: rectangle.
deejt
deejt 2021-6-5
编辑:deejt 2021-6-5
Thank you for your response.
Maybe my problem was not very well explained.
The first block of code you commented on, was never the problem, as I explained with several comments next to it and also the text above. The code line you commenetd on is my matrix that I am plotting and holding on. That section represents 3 lines representing my data, not the rectangle. And I also explained, why I reversed the y axis.
I only posted the code block to present my problem as detailed as possible and provide you guys with a dataset.
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
set(gca,'YDir','reverse') % I need to plot the negative as upward going, therefore I reverse the axis
hold on;
I do see that I made a mistake in the first line, when I posted the question here ( but not in my actual code, so its just a transfer error).
% a = rand([-3.5,1.5],1300,3) % wrong
a = randi([-3,1],3,1300) % this is the correct code line
And the link I sent was mearley meant as a visualization. i do not need the curvate, otherwise I would have asked for it, too (or looked up the doc).
My problem starts after this first code block, namely how can I use existing vector variables to create rectangles, instead of the location input arguments as presented in the rectangle documentation of MATLAB.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2021-6-4
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 个评论
Star Strider
Star Strider 2023-3-10
My pleasure!
A Vote would be appreciated!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by