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

后端 未结 3 766
情歌与酒
情歌与酒 2021-01-12 20:39

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,

3条回答
  •  我在风中等你
    2021-01-12 21:10

    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);
    

提交回复
热议问题