Displaying Marker Values Within a MatLab Plot - matlab

I'm trying to display my values for a given vector within a plot. My code is:
x = [0.1 0.2 -0.1 4.1 -2 1.5 -0.1];
plot(x)
a = num2str(x(:));
b = cellstr(a);
c = strtrim(b);
text(x,y,c);
Its plotting the values, but they are scattered all over the place, and not sitting nicely next to each point on the graph.

As I said in my comment above, calling plot with a single vector input treats the values of the vector as the y-coordinates and their indices as the x-coordinates. Your provided x vector contains negative numbers, but your plot call only has one vector input, so there will be no negative x-coordinates in the plot (there are no negative indices in MATLAB).
Assuming your x vector is your desired y-coordinate, the following example will provide the behavior I'm guessing you're expecting:
y = [0.1 0.2 -0.1 4.1 -2 1.5 -0.1];
x = 1:1:length(y);
plot(x,y)
a = num2str(x(:));
b = cellstr(a);
c = strtrim(b);
h = text(x,y,c);
Where h is an array of object handles that you can use with get and set to query and modify the properties of each individual text object (like size, alignment, etc.).

Related

Contours in Matlab/Octave

I am having some trouble understanding contours.
What I have understood so far is that contours are a way to represent a 3d figure in a 2d plane. It does so by plotting a function of 2 variables as curves along which the function has same value.
Now if I do:
z=[1 4; 10 7];
contour(z);
I get this:
I read the documentation and it says:
contour(Z) draws a contour plot of matrix Z, where Z is interpreted as
heights with respect to the x-y plane. Z must be at least a 2-by-2
matrix that contains at least two different values. The x values
correspond to the column indices of Z and the y values correspond to
the row indices of Z. The contour levels are chosen automatically.
Thus for x=1,y=1: z=1, x=2,y=1: z=4 and so on. However I can't understand how to interpret this as the contour plot shown above.
And if I write:
contour(X1, X2, vals, [0.5 0.5], 'b'); where X1, X2 and vals are equal sized matrices and vals is a matrix of only 0s and 1s. I can't understand what does the argument [0.5 0.5] do. I read the documentation which states:
contour(Z,v) draws a contour plot of matrix Z with contour lines at
the data values specified in the monotonically increasing vector v. To
display a single contour line at a particular value, define v as a
two-element vector with both elements equal to the desired contour
level.
and I am unable to understand this statement.
The problem of the first contour is that there are just 4 values. Try something like
x = 0:0.1:10;
y = 0:0.1:10;
z = sin(x') * cos(y);
contour(z)
For the second thing, this means that if you want to see just particular contours, input them as vector v. In the example above:
contour(z, [0.1, 0.2, 0.3])
will show contour lines of 0.1, 0.2 and 0.3.
To have a single contour line, you can't have just (z, 0) but require (z, [0,0])

Plotting a linear decision boundary

I have a set of data points (40 x 2), and I've derived the formula for the decision boundary which ends up like this :
wk*X + w0 = 0
wk is a 1 x 2 vector and X is a 2 x 1 point from the data point set; essentially X = (xi,yi), where i = 1,2,...,40. I have the values for wk and w0.
I'm trying to plot the line wk*X + w0 = 0 but I have no idea how to plot the actual line. In the past, I've done this by finding the minimum and maximum of the data points and just connecting them together but that's definitely not the right approach.
wk*X is simply the dot product between two vectors, and so the equation becomes:
w1*x + w2*y + w0 = 0
... assuming a general point (x,y). If we rearrange this equation and solve for y, we get:
y = -(w1/w2)*x - (w0/w2)
As such, this defines an equation of the line where the slope is -(w1/w2) with an intercept -(w0/w2). All you have to do is define a bunch of linearly spaced points within a certain range, take each point and substitute this into the above equation and get an output. You'd plot all of these output points in the figure as well as the actual points themselves. You make the space or resolution between points small enough so that we are visualizing a line when we connect all of the points together.
To determine the range or limits of this line, figure out what the smallest and largest x value is in your data, define a set of linearly spaced points between these and plot your line using the equation of the line we just talked about.
Something like this could work assuming that you have a matrix of points stored in X as you have mentioned, and w1 and w2 are defined in the vector wk and w0 is defined separately:
x = linspace(min(X(:,1)), max(X(:,1)));
y = -(wk(1)/wk(2))*x - (w0/wk(2));
plot(X(:,1), X(:,2), 'b.', x, y);
linspace determines a linearly spaced array of points from a beginning to an end, and by default 100 points are generated. We then create the output values of the line given these points and we plot the individual points in blue as well as the line itself on top of these points.

Find actual plot limits when specifying partially automatic limits

How do I find the actual y-range here:
This is just the example from the docs http://www.mathworks.com/help/matlab/ref/axis.html
x = linspace(-10,10,200);
y = sin(4*x)./exp(.1*x);
plot(x,y)
axis([-10 10 0 inf])
the ymin value is specified as zero and the max left automatic. If I now query the range with
get(gca,'YLim')
I just get [ 0 inf ]. How do i determine the actual plot y range used ( it is about [0 2.5] for this example..)
edit - aside
In case anyone else encounters this - it may be preferable to avoid the issue: make the plot with fully automatic ranging then fix the range as you like so you know exactly what it is, eg.
plot(x,y)
origYrange=ylim
origXrange=xlim
axis([origXrange 0 origYrange(2)])
Although the documentation doesn't tell, it appears that when the first (second) value of ylim is set to -inf (inf) Matlab sets the lower (upper) y-axis limit as the minimum (maximum) of all y values in the plot. The latter can be known by reading the 'YData' property of all 'children' of the axis.
yd = get(get(gca,'children'),'YData'); %// get y data of all plots
if iscell(yd) %// if there's more than one plot yd is a cell array of numeric vectors;
%// otherwise it's a numeric vector
yd = [yd{:}]; %// combine all values into a single numeric vector
end
ydminmax = [min(yd) max(yd)]; %// computed limits
result = ylim;
ind = isinf(result);
result(ind) = ydminmax(ind); %// replace infinite values by computed values
In your example, the result is
result =
0 2.4313

drawing 3d contour plot from 3d vector

I want to draw a contour plot for 3D data.
I have a force in x,y,z directions I want to plot the contour3 for that
the dimensions of the Fx = 21x21X21 same for Fy and Fz
I am finding force = f*vector(x,y,z)
Then
Fx(x,y,z) = force(1)
Fy(x,y,z) = force(2)
Fz(x,y,z) = force(3)
I did the following but it is not working with me ?? why and how can I plot that
FS = sqrt(Fx.^2 + Fy.^2 + Fz.^2);
x = -10:1:10;
[X,Y] = meshgrid(x);
for i=1:length(FS)
for j = 1:length(FS)
for k=1:length(FS)
contour3(X,Y,FS(i,j,k),10)
hold on
end
end
end
This is the error I am getting
Error using contour3 (line 129)
When Z is a vector, X and Y must also be vectors.
Your problem is that FS is not the same shape as X and Y.
Lets illustrate with a simple example:
X=[1 1 1
2 2 2
3 3 3];
Y=[1 2 3
1 2 3
1 2 3];
Z=[ 2 4 5 1 2 5 5 1 2];
Your data is probably something like this. How does Matlab knows which Z entry corresponds to which X,Y position? He doesnt, and thats why he tells you When Z is a vector, X and Y must also be vectors.
You could solve this by doing reshape(FS,size(X,1),size(X,2)) and will probably work in your case, but you need to be careful. In your example, X and Y don't seem programatically related to FS in any way. To have a meaningful contour plot, you need to make sure that FS(ii,jj,k)[ 1 ] corresponds to X(ii,jj), else your contour plot would not make sense.
Generally you'd want to plot the result of FS against the variables your are using to compute it, such as ii, jj or k, however, I dont know how these look like so I will stop my explanation here.
[ 1 ]: DO NOT CALL VARIABLES i and j IN MATLAB!
I'm not sure if this solution is what you want.
Your problem is that contour and contour3 are plots to represent scalar field in 2D objects. Note that ball is 2D object - every single point is defined by angles theta and phi - even it is an object in "space" not in "plane".
For representation of vector fields there is quiver, quiver3, streamslice and streamline functions.
If you want to use contour plot, you have to transform your data from vector field to scalar field. So your data in form F = f(x,y,z) must be transformed to form of H = f(x,y). In that case H is MxN matrix, x and y are Mx1 and Nx1 vectors, respectively. Then contour3(x,y,H) will work resulting in so-called 3D graph.
If you rely on vector field You have to specify 6 vectors/matrices of the same size of corresponding x, y, z coordinates and Fx, Fy, Fz vector values.
In that case quiver3(x,y,z,Fx,Fy,Fz) will work resulting in 6D graph. Use it wisely!
As I comment the Ander's answer, you can use colourspace to get more dimensions, so You can create 5D or, theoretically, 6D, because you have x, y, z coordinates for position and R, G, B coordinates for the values. I'd recommend using static (x,y,R,G,B) for 5D graph and animated (x,y,t,R,G,B) for 6D. Use it wisely!
In the example I show all approaches mentioned above. i chose gravity field and calculate the plane 0.25 units below the centre of gravity.
Assume a force field defined in polar coordinates as F=-r/r^3; F=1/r^2.
Here both x and yare in range of -1;1 and same size N.
F is the MxMx3 matrix where F(ii,jj) is force vector corresponding to x(ii) and y(jj).
Matrix H(ii,jj) is the norm of F(ii,jj) and X, Y and Z are matrices of coordinates.
Last command ensures that F values are in (-1;1) range. The F./2+0.5 moves values of F so they fit into RGB range. The colour meaning will be:
black for (-1,-1,-1),
red for (1,-1,-1),
grey for (0,0,0)
Un-comment the type of plot You want to see. For quiver use resolution of 0.1, for other cases use 0.01.
clear all,close all
% Definition of coordinates
resolution=0.1;
x=-1:resolution:1;
y=x;
z=-.25;
%definition of matrices
F=zeros([max(size(x))*[1 1],3]); % matrix of the force
X=zeros(max(size(x))*[1 1]); % X coordinates for quiver3
Y=X; % Y coordinates for quiver3
Z=X+z; % Z coordinates for quiver3
% Force F in polar coordinates
% F=-1/r^2
% spherical -> cartesian transformation
for ii=1:max(size(x))
for jj=1:max(size(y))
% temporary variables for transformations
xyz=sqrt(x(ii)^2+y(jj)^2+z^2);
xy= sqrt(x(ii)^2+y(jj)^2);
sinarc=sin(acos(z/xyz));
%filling the quiver3 matrices
X(ii,jj)=x(ii);
Y(ii,jj)=y(jj);
F(ii,jj,3)=-z/xyz^2;
if xy~=0 % 0/0 error for x=y=0
F(ii,jj,2)=-y(jj)/xyz/xy*sinarc;
F(ii,jj,1)=-x(ii)/xyz/xy*sinarc;
end
H(ii,jj)=sqrt(F(ii,jj,1)^2+F(ii,jj,2)^2+F(ii,jj,3)^2);
end
end
F=F./max(max(max(F)));
% quiver3(X,Y,Z,F(:,:,1),F(:,:,2),F(:,:,3));
% image(x,y,F./2+0.5),set(gca,'ydir','normal');
% surf(x,y,Z,F./2+.5,'linestyle','none')
% surf(x,y,H,'linestyle','none')
surfc(x,y,H,'linestyle','none')
% contour3(x,y,H,15)

Matlab surface plot not giving desired results

I am charting the following data:
a=[...
0.1, 0.7, 0.00284643369242828;...
0.1, 0.71, 0.00284643369242828;...]
such that column 1 never surpasses approximately 10
also such that column 2 goes from .7 to 1.
Column 3 seems ok
When i chart my surface using surf(a) it looks like this:
it appears not to be properly considering what should be x and y.
anything seem weird there?
I think you need to try one of two things: either break out your height column into its own rectangular matrix Z and use surf(Z) to plot each point relative to its location in the matrix (so your x- and y-axes will not be scaled the way you want), or you can put your desired x- and y-coordinates in their own vectors, and plot the matrix Z (defined at every point (xi, yj) for all i in N and j in M where x is N elements long and y is M elements long) with surf(x,y,Z).
x = 0.1:0.1:10; % or whatever increment you need
y = 0.7:0.01:1; % or whatever increment you need
Z = zeros(length(x),length(y); % initialized to the correct size, fill with data
I think you are going to have to regenerate your Z-data so that it is in a rectangular matrix that is (elements in x) by (elements in y) in dimension.
EDIT: You do not need to recreate your data. If you know that you have n unique elements in x and m unique elements in y, then you can use:
X = reshape(data(:,1),m,n);
Y = reshape(data(:,2),m,n);
Z = reshape(data(:,3),m,n);
surf(X,Y,Z);
And that should give you what you are looking for.