需要帮助绘制点之间的线

时间:2010-07-17 05:56:21

标签: matlab

我需要帮助绘制点之间的线条。 假设,我从创建6个随机点开始 -

x = rand(6,1);
y = rand(6,1);

所以我的观点是(x(1),y(1)), (x(2),y(2)), (x(3),y(3)), (x(4),y(4)), (x(5),y(5)), (x(6),y(6))

现在我想在点1和点之间绘制直线。 5,2和& 6,3& 4 并将它们绘制在一个图表中。所以我得到3条直线。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

您可以通过拨打PLOT来完成此操作。如果您将xy数据重新整形为矩阵,每列包含一行坐标,那么PLOT会为每列绘制不同的彩色线:

index = [1 2 3; 5 6 4];   %# The index to reshape x and y into 2-by-3 matrices
plot(x(index),y(index));  %# Plot the lines

答案 1 :(得分:1)

以下是两种方法:

首先,使用hold on。这些线是分开的,即如果你变成一个红色,其他线将保持蓝色。

%# plot the first line
plot([x(1);x(5)],[y(1);y(5)]);

hold on %# this will prevent the previous plot from disappearing

%# plot the rest
plot([x(2);x(6)],[y(2);y(6)]);
plot([x(3);x(4)],[y(3);y(4)]);

第二种方式,利用NaN未被绘制的事实。这些行被分组,即如果你变成一个红色,则所有行都是红色的。

%# create array for plotting
xy = NaN(8,2);

%# fill in data
xy([1 2 4 5 7 8],1) = x([1 5 2 6 3 4]);
xy([1 2 4 5 7 8],2) = y([1 5 2 6 3 4]);

%# plot
plot(xy(:,1),xy(:,2))
相关问题