在简单的 x-y 图中连接最终点和初始点(绘制闭合曲线/多边形) [英] Connecting final and initial point in simple x-y plot (Plotting closed curve/polygon)

查看:32
本文介绍了在简单的 x-y 图中连接最终点和初始点(绘制闭合曲线/多边形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如说,我有...

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

我将得到一个连接点 (1,1)、(1,2) 和 (2,2) 的线的图.有没有办法把最后一个点和第一个点连接起来,从而完成图上的方块?

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?

我也在用点拉入文本行,所以简单地添加另一个点 1,1 不是一种选择.

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

推荐答案

impoly 可能很有用,但是,它创建了比 plot 慢的可修改曲线.

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

顺便说一下,(:) 冒号运算符用作防御性编程手段.这样,xy 既可以是行向量,也可以是列向量.

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.

varargin 允许使用额外的参数,例如:

The varargin allows using additional parameters, like:

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

这篇关于在简单的 x-y 图中连接最终点和初始点(绘制闭合曲线/多边形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆