Search code examples
3dgnuplotoctave

Incorrect GNU Octave 3D Plot


I was trying to draw four points in 3D using GNU Octave. So I began by defining them first.

a = [1 2 3];
b = [-1 0 -1];
c = [5 4 3];
d = [1 0 -2];

Next, I tried with plot3(a,b,c,d, 'o') but I quickly abandoned the idea. Instead I rather used two times plot3 function dividing it with hold on.

plot3(a,b,'o')
hold on
plot3(c,d);

And here's what happened:enter image description here Instead of four points this figure depicts 6 points which are coplanar.

Can somebody please explain what I did wrong?


Solution

  • As the documentation very clearly states, the inputs to the function are the x, y, and z coordinates. If arrays are used, a single point is created for each corresponding point in these vectors. In your case, each input that you are providing to plot3 has three elements and is therefore going to create three points. Since you are calling plot3 twice like this, the result is going to be 6 points.

    The correct usage of plot3 would be to put all of your x values in an array, y values in an array, and z values in an array.

    plot3([a(1), b(1), c(1), d(1)], [a(2), b(2), c(2), d(2)], [a(3), b(3), c(3), d(3)], 'o')
    

    Or maybe more concisely

    points = [a; b; c; d];
    plot3(points(:,1), points(:,2), points(:,3), 'o');