c++ - CERN 根目录 : Is is possible to plot pairs of x-y data points?

标签 c++ graph plot gnuplot root-framework

我想使用 CERN ROOT 绘制 x-y 数据点对的二维图可能还带有 y-errorbars。但是我只会画直方图。

CERN ROOT 是否可行?如果是怎么办?

我还意识到可能有更好的库可以做到这一点。我一直在使用 GNUPlot,但不幸的是我似乎无法将它与我的 C++ 代码很好地集成,因为我找不到涵盖所有功能并允许我以双向方式发送数据的 C/C++ GNUPlot 接口(interface) -即:往返于 GNUPlot。

如果您有更好的替代建议,我们将非常欢迎。

最佳答案

gnuplot iostream将数据从 C++ 发送到 gnuplot。在 root 中,您可以使用(按照其他人的建议)TGraphTGraphErrorsTGraphAsymErrors

编辑:

主页上的 gnuplot iostream 示例如下所示。 意味着一旦您将数据点作为一个元组 vector 或多个浮点 vector ,您就可以将它们发送到 gnuplot。

#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));

    return 0;
}

关于c++ - CERN 根目录 : Is is possible to plot pairs of x-y data points?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654406/

相关文章:

c++ - 条件运算符的返回值

c++ - 无法将 valarray 初始化为类的私有(private)成员

c++ - 在 C++ 中读取和写入同一个二进制文件

algorithm - 开发线性时间算法来遍历图

python - 如何使用 networkx 从给定图中提取所有可能的诱导子图

python - matplotlib Pandas 图中的附加 "axis like"图

python - 如何在 python 中创建 3D 高度图

C++ 风格从 unsigned char * 转换为 const char *

algorithm - 找到一个完美的匹配或证明这是不可能的

python - 比较单个图 python 上的多年数据