我应该如何格式化我的 .dat 文件以便可以制作 3D 矢量绘图? [英] How should I format my .dat file so that a 3D vector plot can be made?

查看:24
本文介绍了我应该如何格式化我的 .dat 文件以便可以制作 3D 矢量绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学完成这项编程任务,我们必须编写一个 c++ 程序来计算 3D 空间中某些线圈的磁场矢量.

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.

我习惯于从 c++(通过管道)调用 gnuplot,这就是我通常做的:

I'm used to calling gnuplot from c++ (via piping) and this is what I usually do:

  1. 创建一个将数据写入.dat 文件的输出流
  2. 打开一个 gnuplot 管道
  3. 使 gnuplot 绘制 .dat
  4. 的所有内容
  1. create an output stream that writes the data to a .dat file
  2. open a gnuplot pipe
  3. make gnuplot plot all the contents of the .dat

由于我的数据一直是 2D、xy 绘图,我在这里很迷茫.我的问题是:

Since my data has always been 2D, xand y plots, I'm quite lost here. My question is:

  1. 如何格式化 .dat 文件(例如,我是否使用大括号对矢量组件进行分组?)
  2. 绘制 3D 矢量场的实际 gnuplot 命令是什么?
  1. How to format the .dat file (e.g. do I use braces to group vector components?)
  2. what is the actual gnuplot command to plot a 3D vector field?

如果我可以像这样格式化 .dat 文件会很容易:

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 ...

当点P=(1,0,2)的磁场矢量等于一个矢量B=(0.7,0.5,0.25).这将很容易编程,真正的问题是:这会做吗?以及如何在 gnuplot 中绘制它.(哇,我已经问了 3 次同样的问题了).

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).

好的,因为有人让我描述我如何管道(不知道这是否是正确的想法)东西到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:

  1. 首先打开一个管道并命名为pipe:

FILE *pipe = popen("gnuplot -persist 2>/dev/null", "w");

  • 通过管道告诉 gnuplot 要做什么:

    fprintf(pipe, "set term x11 enhanced 
    ");
    fprintf(pipe, "plot x^2 ti 'x^2' with lines
    ");
    

    注意 ,这是绝对必要的.它是执行命令的原因.

    notice the which is absolutely necessary. It is what executes the command.

    关闭管道:

    pclose(pipe);
    

  • 我相信这个必要的库叫做 .

    The necessary library is called <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
    

    绘制它的命令是:

    gnuplot> splot "./data3d.dat" with vectors
    

    现在您应该阅读官方手册的第 44 节第 53 页(这里是pdf).您可能会发现这个网站也非常有用.

    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.

    此命令不符合您的描述:将 从 (x,y,z) 映射到 (t,u,v).它实际上做了这个映射:from (X,Y,Z) to (X+dX,Y+dY,Z+dZ).

    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).

    干杯,贝科

    这篇关于我应该如何格式化我的 .dat 文件以便可以制作 3D 矢量绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆