我应该如何格式化 .dat 文件以便制作 3D 矢量图?
我正在为大学完成这项编程任务,我们必须编写一个 c++
程序来计算 3D 空间中某些线圈的磁场矢量。
我已经成功编写了这个程序,并且我认为它运行得很好。
不过,我想添加一个特殊的想法(这是我的试卷,所以它必须特别好!):我不想绘制向量。
我习惯于从 c++
调用 gnuplot
(通过管道),这就是我通常做的事情:
- 创建一个将数据写入
.dat 的输出流
文件 - 打开一个 gnuplot 管道,
- 使 gnuplot 绘制
.dat
的所有内容
,因为我的数据一直是 2D、x
和 y
> 情节,我在这里很迷路。我的问题是:
- 如何格式化
.dat
文件(例如,我是否使用大括号对矢量分量进行分组?) - 绘制 3D 矢量场的实际 gnuplot 命令是什么?
如果我可以像这样格式化 .dat
文件,那就很容易了:
# Px Py Pz Bx By Bz
1 0 2 0.7 0.5 0.25 #<= example data line
... more data ...
当点 P=(1,0,2)
中的磁场矢量等于矢量时B=(0.7,0.5,0.25)
。这很容易编程,真正的问题是:这行吗?以及如何在 gnuplot 中绘制它。 (哇,我想我已经问过同样的问题 3 次了)。
管道到gnuplot
好的,因为有人让我描述一下我如何管道(不知道这是否是正确的术语想法)东西到gnuplot
。如下:
首先打开一个管道并将其命名为
pipe
:FILE *pipe = popen("gnuplot -persist 2>/dev/null", "w");
告诉
gnuplot
通过管道做什么:fprintf(pipe, "设置术语 x11 增强 \n"); fprintf(pipe, "用线绘制 x^2 ti 'x^2'\n");
注意
\n
这是绝对必要的。它是执行命令的东西。关闭管道:
pclose(管道);
我相信必要的库称为
。
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 called <fstream>
I believe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了这个简单的例子来向您展示如何绘制矢量场。输出将类似于下图:
我用来绘制此图的数据示例是
: 现在你应该阅读官方手册的第44
节第53页(这里pdf)。您可能会发现此网站也非常有用。
编辑:
此命令不符合您的描述:将
从 (x,y,z) 映射到 (t,u,v)
。它实际上执行此映射:从 (X,Y,Z) 到 (X+dX,Y+dY,Z+dZ)
。干杯,
贝科
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:
The command to plot it is:
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