How to divide a plot into different regions
17 views (last 30 days)
Show older comments
I have a random set of points which I intend to plot in different colours depending on their region. I am struggling on what function/equation i would need to create the lines/regions. An example of the regions i have plotted below. I would be looking to plot different colours in the 4 different regions i have quickly shown in the image below.
0 Comments
Answers (1)
Image Analyst
on 4 Nov 2013
Get the y values for each line for the x value of your point that you want to plot. So your point's y value will either be less than or greater than the y value of the line. For each line you will know if it's above or below, so just assign the color of the plot based on that.
if y < yBlackLine && y < yRedLine % y is the y of the point.
% Region 1. Plot point as red.
plot(x, y, 'ro');
elseif y < yBlackLine && y > yRedLine
% Region 2 Plot point as green.
plot(x, y, 'go');
elseif y > yBlackLine && y < yRedLine
% Region 3 Plot point as blue.
plot(x, y, 'bo');
elseif y > yBlackLine && y > yRedLine
% Region 4 Plot point as black
plot(x, y, 'ko');
end
2 Comments
Image Analyst
on 4 Nov 2013
Edited: Image Analyst
on 4 Nov 2013
First, get the endpoints of the lines. Then plot them with line():
line([x1, x2], [y1, y2], 'Color', 'r');
Once you have that, you can calculate the slope
slope = (y2-y1) / (x2-x1);
Then the y value for any x value is
y = slope * (x-x1) + y1
Do that for both lines. Plug in the x value for the point you want to plot to find the y value for each line. Then determine if it's above or below the y value of the point you want to plot, like I showed.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!