CERN ROOT:可以绘制成对的x-y数据点吗? [英] CERN ROOT: Is is possible to plot pairs of x-y data points?

查看:58
本文介绍了CERN ROOT:可以绘制成对的x-y数据点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CERN ROOT绘制x-y数据点对的二维图 可能还带有y-errorbars .但是我只知道如何绘制直方图.

I would like to use CERN ROOT to draw a 2d graph of pairs of x-y datapoints possibly also with y-errorbars. However I only know how to draw histograms.

CERN ROOT有可能吗?如果可以,怎么办?

Is this possible with CERN ROOT? If so how?

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

Also I realize that there may be better libraries for doing this. I have been using GNUPlot, but unfortunately I can't seem to integrate it well with my C++ code, since I can't find a C/C++ GNUPlot interface which covers all the features and allows me to send data in a bidirectional manner - ie: both to and from GNUPlot.

如果您有更好的替代建议,那将是最受欢迎的.

If you have a better alternative suggestion then that would be most welcome.

推荐答案

gnuplot iostream 将数据从C ++发送到gnuplot.在root用户中,可以使用(如其他建议) TGraph TGraphErrors TGraphAsymErrors .

There is gnuplot iostream to send data from c++ to gnuplot. Within root, you can use (as suggested by the others) TGraph, TGraphErrors, TGraphAsymErrors.

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

the gnuplot iostream example from its homepage looks like this. Means once you have your data points either as one vector of tuples or as several vectors of floats, you can send them to 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;
}

这篇关于CERN ROOT:可以绘制成对的x-y数据点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆