Matlab:向量指定的 x 值处的垂直线

Matlab: vertical lines at x-values specified by vector

我有一个 x 值向量,我想在其中向图形添加垂直线,例如行向量:vec = [1 2 3 4 5]

我知道您可以像这样添加单条垂直线:

plot([1 1],[0 1])

(在 x=1 处给出一条从 y=0 到 y=1 的垂直线)。

但是当我尝试

vec = [1 2 3 4 5]; lowLine = [0 0 0 0 0]; highLine = [1 1 1 1 1]; plot([vec vec],[lowLine highLine])

它没有给出所需的结果,而是给出了一个 z 形。我哪里错了?

为了在一个图中绘制多条线,您需要使用 MATLAB 的 plot 函数将矩阵作为输入处理,并将输入的每一列视为不同的图:

If X and Y are both matrices, then they must have equal size. The plot function plots columns of Y versus columns of X.

因此,为了获得预期的结果,您需要这样写:

vec = [1 2 3 4 5];
lowLine = [0 0 0 0 0];
highLine = [1 1 1 1 1];
plot([vec;vec],[lowLine;highLine])

结果: