How should I draw a line?

2 views (last 30 days)
JooYoung Jeon
JooYoung Jeon on 29 Nov 2021
Commented: Image Analyst on 30 Nov 2021
In the picture below, I would like to draw a line that goes to the left by connecting blue, red, yellow, and purple to the vertical line. What should I do?
  2 Comments
DGM
DGM on 29 Nov 2021
I'm not sure what you mean. Do you want to create a vertical line somewhere and then extrapolate each of those series such that they meet it? Where should the vertical line be?
It might help to sketch out what you want.
JooYoung Jeon
JooYoung Jeon on 29 Nov 2021
I want to draw a line in this way so that it can move along the drawn plot.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 29 Nov 2021
Edited: Image Analyst on 29 Nov 2021
Try getting the left most point of each array and then using plot. Untested code (because you forgot to attach your data):yLeft
% Get the x values of the left most point on the colored lines, and their indexes.
[minxBlue, indexBlue] = min(xBlue);
[minxRed, indexRed] = min(xRed); % Orange, really
[minxYellow, indexYellow] = min(xYellow);
[minxPurple, indexPurple] = min(xPurple);
% Using the index, get the corresponding y value for the leftmost point.
yLeftBlue = yBlue(indexBlue);
yLeftRed = yRed(indexRed);
yLeftYellow = yYellow(indexYellow);
yLeftPurple = yPurple(indexPurple);
% Construct the red line coordinates.
xLeft = [minxBlue, minxRed, minxYellow, minxPurple]
yLeft = [yLeftBlue, yLeftRed, yLeftYellow, yLeftPurple]
% Plot the line between the leftmost points of the 4 colored lines.
plot(xLeft, yLeft, 'r-', 'LineWidth', 7);
Replace variable names to match what you have in your program for the various colored lines.
  3 Comments
DGM
DGM on 30 Nov 2021
The second output argument of min() is the index where the minimum value was located. For the line
[minxBlue, indexBlue] = min(xBlue);
The minxBlue value is the global minimum of the vector xBlue, and it is found at xBlue(indexBlue).
The idea is to find the leftmost values in the xdata for each series, then use that associated index to find the corresponding values in the ydata for each series. This will give you a set of four points (coordinate pairs), one at the leftmost end of each of the curves. The rest is simply taking those four points and plotting a polyline between them.
Image Analyst
Image Analyst on 30 Nov 2021
Yes, thanks @DGM for explaining. The reason I did it this way instead of just using the very first element of the colored x and y is that the very first element is not guaranteed to be the left most data point. It's possible that the first element could be on the right instead of the left. We can't assume that element #1 is the left-most. That's why, to be most general, it had to be done this way.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!