MATLAB: Using plot3 to plot 3d vectors

MATLAB: Using plot3 to plot 3d vectors

3daxisgraphplotvectors

The following code is meant to plot the vectors (4,5,6), (-3,6,-3) and (1,2,3) directed from the origin.
plot3([0 4], [0 5], [0 6], 'k^--',[0 -3], [0 6], [0 -3], 'b^--', [0 1],...
[0 2],[0 3], 'r^-')
xlabel('x'); ylabel('y'); zlabel('z');
In the plot generated, the location of some points does not appear to correspond to the axis numbers. For example, the origin is located at a point where neither x nor y nor z appear to be 0 although the same is not true for the remaining points on the plot (like (-3,6,-3) and (4,5,6)) and the location of points relative to each other seems correct. Can someone explain what is causing this and how this issue can be fixed?

Best Answer

  • This one is the correct way of plotting the data points:
    point1 = [4,5,6];
    point2 = [-3,6,-3];
    point3 = [1 2 3];
    origin = [0,0,0];
    figure;hold on;
    plot3([origin(1) point1(1)],[origin(2) point1(2)],[origin(3) point1(3)],'r-^', 'LineWidth',3);
    plot3([origin(1) point2(1)],[origin(2) point2(2)],[origin(3) point2(3)],'g-^', 'LineWidth',3);
    plot3([origin(1) point3(1)],[origin(2) point3(2)],[origin(3) point3(3)],'b-^', 'LineWidth',3);
    grid on;
    xlabel('X axis'), ylabel('Y axis'), zlabel('Z axis')
    set(gca,'CameraPosition',[1 2 3]);
  • Add a Comment

    Your email address will not be published. Required fields are marked *