How to plot a vector (array) as a vertical line?

13 ビュー (過去 30 日間)
BM
BM 2018 年 2 月 26 日
編集済み: BM 2018 年 2 月 27 日
Say I have, from my data, a vector x, where x is a 1 x n row vector. How do I plot this as a vertical column vector on an already existing plot? I want to compare data in a specific region of a contour plot. I need to know which region of the contour plot I am extracting my data so that I can be absolutely sure I am comparing the correct regions of data, hence why I want to graph vertical lines on the plot.
So, for example, if:
clear all;
close all;
clc;
a = [1,2,3,3,1;2,3,4,4,2;3,7,7,7,3;4,11,19,23,4;5,70,4,30,5];
%Get size of rows of above matrix
vec = 1:size(a,1);
%Pick out all columns of matrix in row 3
q = a(2,:);
%Plot Contour
contour(a)
hold on
plot(vec(2),q)
hold off
I intend to use the array I called 'q' for further analysis in my real program, but I want to make sure I am extracting the correct vector that I want to analyse. For example, in my example plot here, I want to investigate the vector that is giving me the sharp points at the bottom tip of the series of symmetric cheverons that one can see in the plot of this program, which appear to be at the graphical mark of x = 2 on the plot. Not only do I want to extract that vector, but I want to have a visual cue that I am indeed taking the correct vector from my contour plot, which I can currently only tell by visualization, hence my desire to plot a vertical line to indicate that I have extracted the equivalent of all of the values in the matrix that are expressed along that vertical line. So, I want to extract a entire 'y-array' for a given value of 'x' on my contour plot, and visually plot the vertical line I extracted, to visually see where that extracted array is located in the contour plot. Hopefully I didn't write this in a confusing manner.
  1 件のコメント
BM
BM 2018 年 2 月 27 日
編集済み: BM 2018 年 2 月 27 日
if it further helps my explanation, the x position should be constant, where as the y values should take on every value in the row I extract. The normal plot function is obviously giving me issues because the vertical line plot will not be single-valued for a given x-value. The visualization will just serve as a good check that I am extracting an array of 'y-values' on the plot that are in a region I want to further analyze.

サインインしてコメントする。

回答 (1 件)

BM
BM 2018 年 2 月 27 日
編集済み: BM 2018 年 2 月 27 日
Ok, I think I found a solution, but I would definitely appreciate some feedback to make sure my solution is in line with what I am trying to accomplish. My modified code is as follows:
clear all;
close all;
clc;
a = [1,2,3,3,1;2,3,4,4,2;3,7,7,7,3;4,11,19,23,4;5,70,4,30,5];
%Get size of rows of above matrix (will use as an index for the x-axis)
vec = 1:size(a,1);
%Pick out all columns of matrix in row 3
q = a(2,:);
%Create a vector of all ones the length of the matrix (n in m x n representation of a matrix)
onesvec = ones(1,length(a));
%Create a new vector of all the same x-axis index value
newvec = vec(2)*onesvec;
%Plot Contour
contour(a)
hold on
plot(newvec,q,'r*')
hold off
From the current plot, one can clearly see I now have my vertical vector on the contour plot (indicated by the 'red stars'), as I wanted. Now, this vertical vector does not extend the entirety of the graph, simply because the values it is plotting do not extend the entire range of the plot. Now, having the vertical line extend the full length of the plot is not entirely necessary, as I have accomplished my goal of visually seeing where I extracted my 'q-vector'. Would anybody agree that my 'q-vector' in the plot is the entire 'y-vector' represented by the line of red stars on my graph, even though the line of red stars does not cover the entire range of the contour plot? The 'q-vector' values are the most important here! They all need to be accounted for, and I think this works. Would anyone be able to confirm if they agree with me?
  4 件のコメント
Stephen23
Stephen23 2018 年 2 月 27 日
編集済み: Stephen23 2018 年 2 月 27 日
@BMor: you called contour with one input argument, which means that the X and Y axes are simply the indices of those dimensions of the input matrix. This means the Y range is
[1,size(a,1)]
If you want the entire vector of all Y values that have been plotted then:
1:size(a,1)
You can easily plot these against the vector q, if that is what you are aiming for.
BM
BM 2018 年 2 月 27 日
編集済み: BM 2018 年 2 月 27 日
Perhaps a picture might help. I apologize if my explanation was a bit confusing.
I tried to get the best 'quick' vertical line I could in GIMP. What I want to do is this above. My vector q should contain all of the 'y-values' along this line. Afterwards, I want to graph a line like this on my contour plot that shows where the values in my vector 'q' correspond to on my contour plot. I probably should have started with this explanation, my apologies.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by