c++ - 连接c++文件和gnuplot并在执行后自动绘图

标签 c++ gnuplot

<分区>

有什么方法(说明)可以将gnuplot 连接到C++ 代码,因此它可以在C++ 文件执行结束时自动绘制文件,而无需通过gnuplot 中的传统绘图步骤:打开gnuplot,等

我正在寻找是否有任何指令可以从 C++ 代码调用 gnuplot 绘图选项,以便它会在编译结束后立即绘制文件。

最佳答案

您可以使用 iostream pipe .

例子:

// Demo of vector plot.
// Compile it with:
//   g++ -o example-vector example-vector.cc -lboost_iostreams -lboost_system -lboost_filesystem

#include <vector>
#include <cmath>
#include <boost/tuple/tuple.hpp>

#include "gnuplot-iostream.h"

int main() {
    Gnuplot gp;
    // Create a script which can be manually fed into gnuplot later:
    //    Gnuplot gp(">script.gp");
    // Create script and also feed to gnuplot:
    //    Gnuplot gp("tee plot.gp | gnuplot -persist");
    // Or choose any of those options at runtime by setting the GNUPLOT_IOSTREAM_CMD
    // environment variable.

    // Gnuplot vectors (i.e. arrows) require four columns: (x,y,dx,dy)
    std::vector<boost::tuple<double, double, double, double> > pts_A;

    // You can also use a separate container for each column, like so:
    std::vector<double> pts_B_x;
    std::vector<double> pts_B_y;
    std::vector<double> pts_B_dx;
    std::vector<double> pts_B_dy;

    // You could also use:
    //   std::vector<std::vector<double> >
    //   boost::tuple of four std::vector's
    //   std::vector of std::tuple (if you have C++11)
    //   arma::mat (with the Armadillo library)
    //   blitz::Array<blitz::TinyVector<double, 4>, 1> (with the Blitz++ library)
    // ... or anything of that sort

    for(double alpha=0; alpha<1; alpha+=1.0/24.0) {
        double theta = alpha*2.0*3.14159;
        pts_A.push_back(boost::make_tuple(
             cos(theta),
             sin(theta),
            -cos(theta)*0.1,
            -sin(theta)*0.1
        ));

        pts_B_x .push_back( cos(theta)*0.8);
        pts_B_y .push_back( sin(theta)*0.8);
        pts_B_dx.push_back( sin(theta)*0.1);
        pts_B_dy.push_back(-cos(theta)*0.1);
    }

    // Don't forget to put "\n" at the end of each line!
    gp << "set xrange [-2:2]\nset yrange [-2:2]\n";
    // '-' means read from stdin.  The send1d() function sends data to gnuplot's stdin.
    gp << "plot '-' with vectors title 'pts_A', '-' with vectors title 'pts_B'\n";
    gp.send1d(pts_A);
    gp.send1d(boost::make_tuple(pts_B_x, pts_B_y, pts_B_dx, pts_B_dy));

#ifdef _WIN32
    // For Windows, prompt for a keystroke before the Gnuplot object goes out of scope so that
    // the gnuplot window doesn't get closed.
    std::cout << "Press enter to exit." << std::endl;
    std::cin.get();
#endif
}

阅读Gnuplot Links更多接口(interface)。

关于c++ - 连接c++文件和gnuplot并在执行后自动绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958619/

相关文章:

c++ - 错误 : 'object' was not declared in this scope

linux - gnuplot 基本绘图问题

plot - 有 "automatic"x

3d - 在 gnuplot 中在 pm3d 表面绘制点

GNUplot - 如何将图像作为关键?

php - 在有限资源上开发服务器的最合适方法

c++ - 在 C++ 中模拟 if __name__ == __main__ 导致错误 "function-like macro is not defined"

c++ - 如何 Hook AllocMem()、FreeMem() 等...调用?它可以是任何类型的技术 dll、可执行文件等

colors - Gnuplot 在 rowstack 直方图中重复颜色

c++ - 在 C++ 中使用 protobuf 对象的 protobuf 映射