Gnuplot_i.hpp C++接口绘制局部变量而不是文件

Gnuplot_i.hpp C++ interface plot local variables instead of a file

本文关键字:局部变量 文件 绘制 接口 hpp C++ Gnuplot      更新时间:2023-10-16

我一直在使用Gnuplot_I.hpp c++接口,它允许你在c ++程序中使用gnuplot。它运行良好,除了我似乎无法控制绘图符号、颜色和其他一些重要的绘图功能。下面的代码片段说明了这个问题:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iomanip>       //io format setw(space)
#include "spline.h"
#include "gnuplot_i.hpp" //Gnuplot class
//
using namespace std;
int main(int argc, char** argv) {
//
vector<double> X(5), Y(5);
X[0]=0.1;
X[1]=0.4;
X[2]=1.2;
X[3]=1.8;
X[4]=2.0;
Y[0]=0.1;
Y[1]=0.7;
Y[2]=0.6;
Y[3]=1.1;
Y[4]=0.9;
//demoFile two colums 6 spaces apart
for (int i = 0; i < 5; ++i ) {
demoFile << std::setw(6) << X[i];
demoFile << std::setw(6) << Y[i];
demoFile << 'n';
}
demoFile.close();
//
Gnuplot g1("");
//
g1.reset_plot();
cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
g1.set_grid();
g1.set_pointsize(4.0);
g1.cmd("set term wxt font ',14';set xtics font 'Times-Roman, 14'; 
set ytics font 'Times-Roman, 14'");
//
g1.cmd("plot 'plotfile.txt' with points pointtype 7 pointsize 2 lc 'red'");
g1.set_style("points").plot_xy(X,Y,"Original");

最后一个绘图命令允许您绘制 X,Y 局部向量变量,但我无法设置点的符号或颜色。有一个set_style命令,但这不适用于设置一些重要的情节特征。您可以使用上面的 cmd(( 命令,该命令允许允许允许这些重要功能的"带有点的点类型 7 点大小 2 lc '红色'"字符串。问题是命令字符串似乎没有办法合并局部向量变量 X 和 Y。命令字符串仅识别强制您在打印之前输出到文件的文件参数。我尝试格式化 gprintf 和 sprintf 字符串以识别局部变量 X 和 Y 但没有成功。有没有人解决了这个问题?

gnuplot_i.hpp 中的 plot_xy(( 例程将 X 和 Y 向量写入临时文件,然后告诉 gnuplot 绘制该文件。 您可以执行完全相同的操作,除了通过g1.cmd发出完整的 gnuplot plot 命令,其中包含您喜欢的所有样式规范,如您在脚本中所示。 那么问题到底是什么呢?

除 plot_xy((:

std::string name = create_tmpfile(tmp);
for (unsigned int i = 0; i < x.size(); i++)
tmp << x[i] << " " << y[i] << std::endl;
tmp.flush();
tmp.close();    
plotfile_xy(name, 1, 2, title);