MATLAB: How to fill a plot section from a single x,y coordinates

data pointfillplot

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

Best Answer

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.