How should I format my .dat file so that a 3D vector plot can be made?
I'm working this programming task for college where we have to write a c++
program that calculates the magnetic field vector for certain coils in 3D space.
I've managed to write this program and I think I've got it working pretty well.
I want to add in a special thinh though (it's my exam paper, so it has to be extra good!): I wan't to plot the vectors out.
I'm used to calling gnuplot
from c++
(via piping) and this is what I usually do:
- create an output stream that writes the data to a
.dat
file - open a gnuplot pipe
- make gnuplot plot all the contents of the
.dat
Since my data has always been 2D, x
and y
plots, I'm quite lost here. My question is:
- How to format the
.dat
file (e.g. do I use braces to group vector components?) - what is the actual gnuplot command to plot a 3D vector field?
It'd be easy if I could format the .dat
file like this:
# Px Py Pz Bx By Bz
1 0 2 0.7 0.5 0.25 #<= example data line
... more data ...
when the magnetic field vector in the point P=(1,0,2)
equals a vector B=(0.7,0.5,0.25)
. This would be easy to program, the real question is: will this do ? and how to I plot it in gnuplot. (wow, I've asked the same question 3 times I guess).
Piping to gnuplot
Ok, since someone asked me to describe how I pipe (don't know if it's the right term thought) stuff to gnuplot
. Here it is:
First open up a pipe and call it
pipe
:FILE *pipe = popen("gnuplot -persist 2>/dev/null", "w");
Tell
gnuplot
what to do through the pipe:fprintf(pipe, "set term x11 enhanced \n"); fprintf(pipe, "plot x^2 ti 'x^2' with lines\n");
notice the
\n
which is absolutely necessary. It is what executes the command.close the pipe:
pclose(pipe);
The necessary library is call开发者_Go百科ed <fstream>
I believe.
I made this simple example to show you how to draw a vector field. The output would be something like this pic:
The data example I used to plot this was:
# Px Py Pz Bx By Bz
0 0 0 0.8 0.8 0.45
0 0 1 0.5 0.7 0.35
0 0 2 0.7 0.5 0.25
0 1 0 0.65 0.65 0.50
0 1 1 0.6 0.6 0.3
0 1 2 0.45 0.45 0.20
1 0 0 0.5 0.7 0.35
1 0 1 0.75 0.75 0.4
1 0 2 0.85 0.85 0.25
1 1 0 0.90 0.85 0.23
1 1 1 0.95 0.86 0.20
1 1 2 0.98 0.88 0.13
2 0 0 0.73 0.83 0.43
2 0 1 0.53 0.73 0.33
2 0 2 0.73 0.53 0.23
2 1 0 0.68 0.68 0.52
2 1 1 0.63 0.57 0.23
2 1 2 0.48 0.42 0.22
The command to plot it is:
gnuplot> splot "./data3d.dat" with vectors
Now you should read the section 44, page 53 of the official manual (and here the pdf). You may find this site also very useful.
Edited:
This command doesn't fit into your description: mapping from (x,y,z) to (t,u,v)
. It actually does this mapping: from (X,Y,Z) to (X+dX,Y+dY,Z+dZ)
.
Cheers, Beco
精彩评论