简体   繁体   English

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

[英]Matlab: vertical lines at x-values specified by vector

I have a vector of x-values at which I would like to add vertical lines to a graph, say a row vector: vec = [1 2 3 4 5] 我有一个x值向量,我想在该向量上向图中添加垂直线,例如行向量: vec = [1 2 3 4 5]

I know that you can add single vertical lines like this: 我知道您可以添加单个垂直线,如下所示:

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

(gives a vertical line at x=1 from y=0 to y=1). (在y = 1到y = 1的x = 1处给出一条垂直线)。

But when I try something like 但是当我尝试类似

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

It does not give the required result, instead it gives a z-shape. 它没有给出所需的结果,而是给出了Z形。 Where am I going wrong? 我要去哪里错了?

In order to plot several lines in a single plot, you need to use the fact that MATLAB's plot function handles matrices as inputs, and that it sees each column of the inputs as different plots : 为了在单个图中绘制多条线,您需要使用以下事实:MATLAB的plot函数将矩阵作为输入处理,并且它将输入的每一列视为不同的图:

If X and Y are both matrices, then they must have equal size. 如果X和Y都是矩阵,则它们必须具有相等的大小。 The plot function plots columns of Y versus columns of X. plot函数绘制Y列与X列的关系。

Thus, in order to get the expected result, you need to write : 因此,为了获得预期的结果,您需要编写:

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

Result : 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM