How can I fill a plot section from a single x,y coordinates?

2 views (last 30 days)
Hello,
I simply want to fill an unbounded section of a plot from an x,y coordinate. For example, the code below creates a vertical line that divides a plot area into two unbounded regions. I would like to fill in the plot area to the left (indicated by the red circle). Is there a simple way to achieve this? Polygons could be a possible solution, but it may get complicated since the line is not always vertical and the data point could be anywhere.
y=0:100;
x=ones(size(y));
plot(x,y)
hold all
scatter(0.5,50,'r')
axis([0 2 0 100])
Thanks

Accepted Answer

Matt Fig
Matt Fig on 21 Aug 2012
Edited: Matt Fig on 21 Aug 2012
I would fill it with a patch object.
line([1 1],[0 100],'color','b','linewidth',3);
hold all
verts = [[0;1;1;0],[0;0;100;100]];
faces = [1 2 3 4];
cdata = [1 0 0];
p = patch('Faces',faces,'Vertices',verts,'FaceColor','flat',...
'FaceVertexCData',cdata,'edgecolor','none');
axis([0 2 0 100])
In the same way, you can make patch out of polygons by specifying the values in the verts array. Column 1 is the x-coord and column two is the y-coord of the vertices of the polygon.
  4 Comments
Matt Fig
Matt Fig on 21 Aug 2012
Edited: Matt Fig on 21 Aug 2012
I see. Tall order indeed. I will advise you with an outline of how I might tackle this.
Somehow you are specifying the regions, so use those coordinates along with the axes limits to create a complete set of polygons. Then when the point is given, check with INPOLYGON which polygon the point is in. It should be in exactly 1 if you have correctly specified the axes divisions. Then make a patch object corresponding the polygon found in the previous step and color it as you wish.
Good luck!

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 21 Aug 2012
Edited: Azzi Abdelmalek on 21 Aug 2012
close;
xmin=0;xmax=2;ymin=0;ymax=100
choice=2
y=0:100;
x=ones(size(y));
plot(x,y)
hold all
axis([xmin xmax ymin ymax]);
n=length(y);
if choice==1
x1=[x xmin xmin x(1)];y1=[y ymax ymin y(1)];
fill(x1,y1,'r')
elseif choice==2
x1=[x xmax xmax x(1)] ;y1=[y ymax ymin y(1)];
fill(x1,y1,'r')
end
  7 Comments
Hawre
Hawre on 21 Aug 2012
Edited: Hawre on 21 Aug 2012
I see. Sorry about that. I meant to color the section that contains the point. Matlab has a "fill" command, but unfortunately, it only works with bounded shapes, such as a polygon.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!