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

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

 Réponse acceptée

Matt Fig
Matt Fig le 21 Août 2012
Modifié(e) : Matt Fig le 21 Août 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 commentaires

Hawre
Hawre le 21 Août 2012
Modifié(e) : Hawre le 21 Août 2012
Thanks Matt. Your solution works great if the line and the section I would like to fill in are fixed. So, it should work for a few graphs. However, could this function be modified so that it can automatically create the boundaries (vertices) based on the line and the datapoint(x,y) that should always be within the section that gets colored?
Matt Fig
Matt Fig le 21 Août 2012
Modifié(e) : Matt Fig le 21 Août 2012
So are you saying that you might have the axes divided up in an arbitrary way and one coordinate pair given. Whatever region of the axes the coordinate pair falls in (it may be bounded by the axes limits), you want that filled. Is this correct?
Yes, this is exactly what I want.
Matt Fig
Matt Fig le 21 Août 2012
Modifié(e) : Matt Fig le 21 Août 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!

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 21 Août 2012
Modifié(e) : Azzi Abdelmalek le 21 Août 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 commentaires

Hawre
Hawre le 21 Août 2012
Modifié(e) : Hawre le 21 Août 2012
Thanks Azzi for the quick response. The only thing missing from this solution is that it needs to be restructured if the x,y co-ordinate is on the other side of the line. For example if x,y is on the right of the dividing line (1.5,50) instead of (0.5,50), the code needs to be readjusted. Also, I am not sure if it will work in cases where the dividing line is horizontal or curved.
if there is a curve the program must change
if choice==1, it fills on the left, if choice==2 it fills on the right. and it may work even it's a curved
Hawre
Hawre le 21 Août 2012
Modifié(e) : Hawre le 21 Août 2012
True, but I don't really know where the point is going to be in relation to the line. It could be above, below, right or left. I would like the program to fill in the section of the plot that the point resides in automatically without me telling it which side its in.
Is this what you mean by 'fill' in your question? I was picturing more like to make the region all one color.
Hawre
Hawre le 21 Août 2012
Modifié(e) : Hawre le 21 Août 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.
you are right, it's better to use fill,

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by