I was using pyroot several years ago and now I was trying to layout some graph unsuccessfully. I'm experiencing a problem with TGraph, something related to the values I'm trying to plot. Below is the code:
import ROOT
from ROOT import TChain, TSelector, TTree, TCanvas, TFile, TProfile, TGraph, TCanvas, TH1F, TH2F, TMultiGraph, TLegend, TH2D, gROOT
import csv
import numpy as np
import os, time, argparse, matplotlib, glob, datetime, sys
import pandas as pd
file = '/path_to_file/file.csv'
data = np.loadtxt(file, delimiter=',', skiprows=1)
print(data[:,0])
n_entries = len(data[:,0])
print(type(data[0,0]))
print(type(data[0,1]))
print(type(n_entries))
c1 = ROOT.TCanvas()
gr = TGraph(n_entries, data[:,0], data[:,1])
gr.SetTitle( 'BLA BLA BLA' )
gr.GetXaxis().SetTitle( 'BLA BLA ' )
gr.GetYaxis().SetTitle( 'BLA' )
gr.Draw( 'A*' )
As far as I know it is possible to use numpy arrays as entries for TGraph. The x and y values have same entries (they came from a .csv file) which I retrieve as n_entries. I also tried to get values with pandas, making use of pd.read_csv (you can see also the import of pandas module) method but the output was exactly the same. the output shown is the following:
<class 'numpy.float64'>
<class 'numpy.float64'>
<class 'int'>
Traceback (most recent call last):
File "/path_to_python_file/plot.py", line 59, in <module>
gr = TGraph(n_entries, data[:,0], data[:,1])
TypeError: none of the 12 overloaded methods succeeded. Full details:
TGraph::TGraph(const TGraph& gr) =>
TypeError: takes at most 1 arguments (3 given)
TGraph::TGraph(const TVectorT<float>& vx, const TVectorT<float>& vy) =>
TypeError: takes at most 2 arguments (3 given)
TGraph::TGraph(const TVectorT<double>& vx, const TVectorT<double>& vy) =>
TypeError: takes at most 2 arguments (3 given)
TGraph::TGraph(const TH1* h) =>
TypeError: takes at most 1 arguments (3 given)
TGraph::TGraph(const TF1* f, const char* option = "") =>
TypeError: takes at most 2 arguments (3 given)
TGraph::TGraph() =>
TypeError: takes at most 0 arguments (3 given)
TGraph::TGraph(int n) =>
TypeError: takes at most 1 arguments (3 given)
TGraph::TGraph(const char* filename, const char* format = "%lg %lg", const char* option = "") =>
TypeError: could not convert argument 1 (bad argument type for built-in operation)
TGraph::TGraph(int n, const double* y, double start = 0., double step = 1.) =>
TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
TGraph::TGraph(int n, const int* x, const int* y) =>
TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
TGraph::TGraph(int n, const float* x, const float* y) =>
TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
TGraph::TGraph(int n, const double* x, const double* y) =>
TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
Thanks in advance