【问题标题】:Draw set of points , lines in Gnuplot在 Gnuplot 中绘制一组点、线
【发布时间】:2019-03-05 12:43:55
【问题描述】:

我认为对于已经使用过 gnuplot 和“splot”命令的人来说,这可能是一个非常简单的问题,但我现在无法弄清楚,因为这是我使用这个程序的第一天。

我已将我的 c++ 项目链接到 gnuplot,因此我能够创建具有以下格式的 Datafile.dat:

# Xcoord Ycoord Zcoord
1 2 1
2 1 1
3 1 1
3 2 2
3 3 3

在我的 C++ 文件中,我这样做了:

#include "gnuplot.h"
#include <iostream>
using namespace std;
int main() {
    Gnuplot plot;
    plot("set border 4095");
    plot("splot \"C:/\\Users/\\lRaulMN/\\Desktop/\\Datafile.dat\" with lines");
    return 0;
}

这很好用,我明白了:

现在的问题是:鉴于我使用的 5 个点,有没有办法在不必创建 Datafile.dat 的情况下对这些数字进行标绘?

因为在未来,我的代码中会有这样的东西:

#include "gnuplot.h"
#include <iostream>
#include <vector>
using namespace std;
typedef struct {
    double Time;
    double X;
    double Y;
    double Z;
} DimensionalPoint;

int main() {
    Gnuplot plot;
    plot("set border 4095"); 
    vector<DimensionalPoint> test;
    plot("splot \"C:/\\Users/\\lRaulMN/\\Desktop/\\Datafile.dat\" with lines");
    return 0;
}

所以我的想法是用数字填充测试向量(在我的 c++ 代码中计算它们),然后以某种方式调用 splot 并表示这些数字。

想到的第一个想法是创建一个带有数字的文件(在 c++ 项目中,在执行时),然后对该文件进行“splot”,但是我会为每次交互创建很多文件(因为我会有几个向量),我不想最终使用这个解决方案。

我想没有一种超级简单的方法可以将 3D 点向量插入到 gnuplot 中,但只要我知道如何用 X、Y 和 Z “绘制”至少两个数字,我就可以处理这个问题坐标。

【问题讨论】:

  • 如果你有 Gnuplot 5+ 版,你可以内联数据。你可以看一个例子here
  • 谢谢大家。我在这些链接中找到了很多信息。我会用解决方案来回答,但你帮了我很多!昨天没找到相关问题^^'

标签: c++ gnuplot


【解决方案1】:

感谢@Thor 和@Bob,我找到了解决方案。

首先我创建一个“doubletoString”方法:

string doubletoString(double value) {
    std::ostringstream origin;
    origin << value;
    std::string str = origin.str();
    return str;
}

然后我创建几个 3D 点来尝试。

vector<DimensionalPoint> test;
DimensionalPoint A, B, C, D;
A.X = 1; A.Y = 1; A.Z = 1;
B.X = 2; B.Y = 2; B.Z = 3;
C.X = 1.2; C.Y = 2.4; C.Z = 2.3;
D.X = 8; D.Y = 3; D.Z = 1;
test.push_back(A);
test.push_back(B);
test.push_back(C);
test.push_back(D);

然后,我将此消息发送到 GNUplot。

plot("set border 4095");
plot("$DATA << EOD"); 
double Xaux, Yaux, Zaux;
for (int i = 0; i < test.size(); i++) {
    Xaux = test.at(i).X;
    Yaux = test.at(i).Y;
    Zaux = test.at(i).Z;        
    plot(doubletoString(Xaux) + " " + doubletoString(Yaux) + " " + 
    doubletoString(Zaux));
}
plot("EOD");
plot("splot $DATA with lines");

这是我执行代码时的绘图结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多