How to specify a dotted line in plot3?

346 Views Asked by At

I am trying to generate a random rotation matrix R and apply it to a vector. At the end, I have to plot both the original vector and the vector after rotation. The original vector has to be plotted with black dashed line, and the vector after rotation has to be plotted with black dotted line. I have done every step correctly except I can't plot the vector after rotation with dots. MATLAB plots only the beginning and last points of the vector, but not fully. Interestingly if I try 'k--' instead of 'k.' it works correctly. Can somebody show what I am missing here?

% rand(3,1) generates a random 3 by one column vector. We use this u to plot
u=rand(3,1)*2-1;

% plot the origin
plot3(0,0,0,'.k')

% axis setting
axis vis3d
axis off

%%%%% your code starts here %%%%%
% generate a random rotation matrix R

[R,N] = qr(randn(3));

% plot the x axis 
plot3([0,1],[0,0],[0,0],'r');
text(1,0,0,'x')

% plot the y axis 
plot3([0,0],[0,1],[0,0],'g');
text(0,1,0,'y')

% plot the z axis 
plot3([0,0],[0,0],[0,1],'b');
text(0,0,1,'z')

% plot the original vector u
plot3([0,u(1)],[0,u(2)],[0,u(3)], 'k--');
text(u(1),u(2),u(3),['(',num2str(u(1),'%.3f'),',',num2str(u(2),'%.3f'),',',num2str(u(3),'%.3f'),')'])
hold on

% apply rotation and calcuate v plot the vector after rotation v
v = R*u;

% plot the new vector v
plot3([0,v(1)],[0,v(2)],[0,v(3)], 'k.');
text(v(1),v(2),v(3),['(',num2str(v(1),'%.3f'),',',num2str(v(2),'%.3f'),',',num2str(v(3),'%.3f'),')'])

%%%%% your code ends here %%%%%

I replaced 'k.' with ':k' and it worked like a charm. However, I don't have any idea about what is going on. Why didn't 'k.' work which?

1

There are 1 best solutions below

0
On

The documentation on plot() clearly specifies this:

. Point
-. Dash-dot line
: Dotted line

Thus k. makes only a black dot as marker (i.e. on the exact points you specified as (x,y,z) coordinates), whereas k: makes a dotted line appear from point to point.

The same syntax works for other plotting commands where you can specify line styles, such as plot3D().