Connecting final and initial point in simple x-y plot (Plotting closed curve/polygon)

一世执手 提交于 2019-12-30 08:41:10

问题


Say, for example, I had ...

x = [1 1 2 2];
y = [1 2 2 1];
plot(x, y, 'b-');

I will get a plot with lines connecting the points (1,1), (1,2), and (2,2). Is there any way to connect the final point with the first, thus completing the square on the plot?

I'm also pulling in lines of text with points, so simply adding another point 1,1 is not an option.


回答1:


impoly can be useful, however, it creates a modifiable curve which is slower than plot.

You can write a simple function for that:

function plotc(x,y,varargin)  
    x = [x(:) ; x(1)];   
    y = [y(:) ; y(1)];  
    plot(x,y,varargin{:})  
end

By the way, the (:) colon operator is used as defensive programming means. In this way, x and y can be either row or column vectors.

The varargin allows using additional parameters, like:

 plotc(x,y,'Color','r');
 plotc(x,y,'Parent',a,'LineWidth',2);



回答2:


Unless your final and last points are the same then plot won't know that you want a closed curve. So either add an additional point to your list to plot or try using, for example, rectangle.




回答3:


Do you have the Image Processing Toolbox? If yes,

 impoly(hparent, position, 'Closed')

might be of use to you.

http://www.mathworks.de/help/toolbox/images/ref/impoly.html



来源:https://stackoverflow.com/questions/8545077/connecting-final-and-initial-point-in-simple-x-y-plot-plotting-closed-curve-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!