Plotting 3D vectors

I’m doing linear algebra with Owl. Owl-plplot works great for visualizing 2D vectors, but it doesn’t seem to capable of plotting 3D vectors.

I took a (quick) look at vanilla Plplot, Oplot, and the GNUplot bindings, but I didn’t find a simple way to plot 3D vectors.

I don’t need high quality plots, 3D surfaces, a lot of control or fancy features, just a coordinate system and some function to draw geometric primitives (points, lines, circles, etc.).

Did I miss anything or do I have to build this myself with the good old Graphics module?

Thank you!

1 Like

What kind of vector representation do you want? Just lines/arrows in 3D? That’s just a curve in 3D, so it should be possible with Owl and plplot, at least. Looks like it should be easy with oplot, too (but I haven’t used oplot). There are some 3D Owl plplot examples, with source code, on these pages:

I don’t know whether it will be easy to adapt them to your needs. I wrote the last example on the last page above. It’s a plot of a series 2D curves in 3D. Maybe some of the techniques can be adapted to your needs. (The code is a few years old. I’m not sure whether it works with the current version of Owl.)

(If you end up having to use low-level bindings to plplot, oplot, etc. from Owl, you might consider contributing a wrapper module that makes it easy to do the kind of plot you want.)

1 Like

Thank you for your answer.

I’d just like to draw 3D vectors in a cartesian coordinate system. A plot should look similar to this:

I wouldn’t even need arrows, simple lines would be ok.

Maybe there is a way to use one of the 3D functions (Plot.surf, Plot.mesh, Plot.contour), but I can’t figure it out.

It’s been a while since I worked with plplot but what you showed should be possible. The plline3 function allows you to plot line segments in 3d space. The function is setup to take multiple segments in a single call. For a single segment each array would hold a single value. Colors can be set between draw calls.

2 Likes

Thank you, plline3 does the trick.

in oplot, there is the Curve3d object that should do it, Plt (oplot.Oplot.Plt)
although it is quite rudimentary

5 Likes

Thank you! Oplot’s API looks better than Plplot’s - I’ll use it next time.

My advice:

  1. make your OCaml program write out the 3D data in some text file
    to read and plot it with gnuplot later on.
  2. Figure out how to plot your data with gnuplot.

Advantages: all the power of gnuplot without having to reprogram it yourself in ocaml.
If later you want to get rid of gnuplot and do with some other library/program; still possible.

I guess that just reading the interface of the OCaml gnuplot bindings will kill pretty fast any will to use them.

That’s right. And it’s also clear to me why Owl’s authors chose to build a wrapper for Plplot’s API. Plplot is very powerful and it’s authors did a great job by providing a lot of examples in a dozen programming languages each. The OCaml code is mostly imperative though.

In case you are interested, this is why I was searching for a 3D vector plotting library. In preparation for my son’s math homeworks (and feeling the urge to play with Owl), I’m working through the book Math for Programmers (I’m not affiliated with the author, this is not an ad).
In it’s early chapters the book uses Python with Numpy, Matplotlib, and OpenGL (Pygame) to do some vector graphics. Of course I wanted to do the exercises with OCaml.

Doing the vector maths with Owl.Mat was mostly overkill, but I enjoyed playing with the API.

Here are some sample pictures: I endet up using Owl-plplot for the 2D exercises, vanilla Plplot for 3D vectors and the plain Graphics module for rendering 3D objects.

Translating the code was fairly easy. Python’s list comprehensions lead to very compact code in some cases. On the other hand, it’s very clunky when it comes to things like function composition and currying.

This was fun, thanks for your help.

3 Likes