对于那些已经使用了gnuplot和“ splot”命令的人,我认为这可能是一个非常简单的问题,但是由于现在是我使用该程序的第一天,我现在无法弄清楚。
我实现了将我的c ++项目链接到gnuplot,因此我可以使用以下格式创建Datafile.dat:
# Xcoord Ycoord Zcoord
1 2 1
2 1 1
3 1 1
3 2 2
3 3 3
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
这完美地工作,我明白了:

现在的问题是:考虑到我正在使用的5点,是否有任何方法可以在不必创建Datafile.dat的情况下绘制这些数字?
Because in the future, I'll have something like this in my code:
#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;
}
Run Code Online (Sandbox Code Playgroud)
So my idea was to fill the test vector with numbers (Calculate them in my c++ code) and then call splot somehow and represent those numbers.
The first idea that came to mind was to create a file with the numbers (in c++ project, while executing), then make "splot" to that file however I'd be creating a lot of files for each interaction (because I'll have several vectors) and I don't want to end up using this solution.
I guess there won't be a super easy way to insert a vector of 3D points into gnuplot, but I can deal with that as soon as I know how to "splot" at least two numbers with X,Y and Z coordinates.
感谢@Thor 和@Bob,我找到了解决方案。
首先,我创建一个“doubletoString”方法:
string doubletoString(double value) {
std::ostringstream origin;
origin << value;
std::string str = origin.str();
return str;
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一些 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);
Run Code Online (Sandbox Code Playgroud)
然后,我将此消息发送到 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");
Run Code Online (Sandbox Code Playgroud)
这是我执行代码时的绘图结果。
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |