Plot multiple columns from txt file

Hello guys, I started to use root to plot data.
I have a data3.txt file that is formatted in this way.
t1,x1,y1,z1
t2,x2,y2,z2

tn,xn,yn,zn

0,-3.995401863840999e-10,0,-3.995401905274687e-10
9.999999999999999e-12,4.997762014691721e-10,9.999999999999999e-12,4.995327580028662e-10
2e-11,3.637950016230157e-09,2e-11,3.637187318331949e-09
3e-11,8.978101986779303e-09,3e-11,8.976621362139576e-09

I implemented this code, pop up the windows, but i don’t see the graph. should be three signal in the same graph. Where do i wrong?

void plot2(){
ifstream infile; // input file handle
infile.open(“data3.txt”); // opening input data file

double x,y,z,w; // variable for storing data points while reading
vector x_list, y_list, z_list, w_list; // vectors for storing data points x and y

while (infile>>x>>y>>z>>w) { // reading data point in while loop
x_list.push_back(x); // adding data point x to vector
y_list.push_back(y); // adding data point y to vector

z_list.push_back(z);        // adding data point z to vector
w_list.push_back(w);        // adding data point w to vector

}

TMultiGraph *mg = new TMultiGraph(); // declaring TMultiGraph pointer

TGraph *g1 = new TGraph(x_list.size(), &x_list[0], &y_list[0]); // creating TGraph for first and second columns
TGraph *g2 = new TGraph(x_list.size(), &x_list[0], &z_list[0]); // creating TGraph for first and third columns
TGraph *g3 = new TGraph(x_list.size(), &x_list[0], &w_list[0]); // creating TGraph for first and fourth columns

g1->SetLineColor(kRed); // set color for first graph
g2->SetLineColor(kGreen); // set color for second graph
g3->SetLineColor(kBlue); // set color for third graph

mg->Add(g1);
mg->Add(g2);
mg->Add(g3);

mg->Draw(“AL”);
mg->GetXaxis()->SetTitle(“X”);
mg->GetYaxis()->SetTitle(“Y”);
mg->SetTitle(“Plot of Second, Third, and Fourth Columns vs First Column”);
}

Welcome to the ROOT forum.

data3.txt

0                     -3.995401863840999e-10 0                     -3.995401905274687e-10
9.999999999999999e-12  4.997762014691721e-10 9.999999999999999e-12  4.995327580028662e-10
2e-11                  3.637950016230157e-09 2e-11                  3.637187318331949e-09
3e-11                  8.978101986779303e-09 3e-11                  8.976621362139576e-09

plo2.C

void plot2(){
   ifstream infile; // input file handle
   infile.open("data3.txt"); // opening input data file

   double x,y,z,w; // variable for storing data points while reading
   vector<double> x_list, y_list, z_list, w_list; // vectors for storing data points x and y

   while (infile>>x>>y>>z>>w) { // reading data point in while loop
      x_list.push_back(x); // adding data point x to vector
      y_list.push_back(y); // adding data point y to vector
      z_list.push_back(z);        // adding data point z to vector
      w_list.push_back(w);        // adding data point w to vector
   }

   TMultiGraph *mg = new TMultiGraph(); // declaring TMultiGraph pointer

   TGraph *g1 = new TGraph(x_list.size(), &x_list[0], &y_list[0]); // creating TGraph for first and second columns
   TGraph *g2 = new TGraph(x_list.size(), &x_list[0], &z_list[0]); // creating TGraph for first and third columns
   TGraph *g3 = new TGraph(x_list.size(), &x_list[0], &w_list[0]); // creating TGraph for first and fourth columns

   g1->SetLineColor(kRed); // set color for first graph
   g2->SetLineColor(kGreen); // set color for second graph
   g3->SetLineColor(kBlue); // set color for third graph

   mg->Add(g1);
   mg->Add(g2);
   mg->Add(g3);

   mg->Draw("AL");
   mg->GetXaxis()->SetTitle("X");
   mg->GetYaxis()->SetTitle("Y");
   mg->SetTitle("Plot of Second, Third, and Fourth Columns vs First Column");
}

Gives:

I am expecting 3 lines, but in my case is opening just the windows without signals.
this should be the result of the first two columns. I used excel to shows the result.
image

I got the problem…
my data are like this:

0,1,2,3
1,2,3,4
2,3,4,5
3,4,5,6
with space is working.
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
How can i manage this comma “,” point during the reading?

Yes thats what i showed in the corrected example I sent you. I reformatted the data file (without commas) and I fixed a mistake in the script.

is there any way to plot the file without modifying the source data?

Plotting the file is not the issue, but reading it is the question. That’s not a ROOT issue but a C++ one. And there quite a lot of details on the web regarding the reading of CSV (Comma Separated Value) files:

Etc …

You can also use this constructor to read the file (instead of ifstream), selecting the columns (using the appropriate separator) you want for each graph:
https://root.cern/doc/master/classTGraph.html#ab902beb9e0463b225f0f384b6826a434

1 Like

I tried this way, but is not working.
I tried also this command: TGraph* gr = new TGraph(“data3.txt”,“%lg %lg %lg %lg”,“,”);

void plot1() {

TGraph* gr = new TGraph(“data3.txt”,“%lg,%lg,%lg,%lg”);
gr->SetTitle(“Distance Time Graph;Time (s); Distance (m)”);

gr->SetMarkerColor(4);
gr->SetLineWidth(3);
gr->SetLineColor(4);
gr->GetXaxis()->CenterTitle(true);
gr->GetYaxis()->CenterTitle(true); //

TCanvas *c1 = new TCanvas(“c1”, “c1”, 83, 137, 894, 713);
c1->SetFillColor(0); // background frame color white
c1->SetGridx(); // set vertical grid
c1->SetGridy(); // set horizzontal grid
c1->SetFrameFillColor(0); // background graph color
c1->SetFrameLineWidth(3);

gr->Draw(); // plotting graph on canvas

}

TGraph has only two vectors X and Y (see documentation) your file has four columns. It can be read that way with TGraphErrors (see documentation). Assuming the last two columns are the errors:

auto  gr = new TGraphErrors("data3.txt","%lg,%lg,%lg,%lg");

I found this other workaround. what do you think?

void plot1() {

//TGraph* gr = new TGraph(“data3.txt”,“%lg,%lg,%lg,%lg”);
// Create a TCanvas to draw the multi-graph
TCanvas *c = new TCanvas(“c”, “Multi-Graph Example”, 800, 600);

// Create a TMultiGraph object
TMultiGraph *mg = new TMultiGraph();

// Read the data file
ifstream file(“data3.txt”);
if (!file) {
cerr << “Error: unable to open file data3.txt” << endl;
return;
}

// Create two TGraph objects to store the data
TGraph *graph1 = new TGraph();
TGraph *graph2 = new TGraph();
TGraph *graph3 = new TGraph();

// Read the data file and fill the TGraph objects
double x, y1, y2, y3;
while (file >> x >> y1 >> y2 >> y3) {
graph1->SetPoint(graph1->GetN(), x, y1);
graph2->SetPoint(graph2->GetN(), x, y2);
graph3->SetPoint(graph2->GetN(), x, y3);
}

// Add the TGraph objects to the TMultiGraph
mg->Add(graph1);
mg->Add(graph2);
mg->Add(graph3);

// Customize the multi-graph
mg->SetTitle(“Multi-Graph Example”);
mg->GetXaxis()->SetTitle(“X Axis”);
mg->GetYaxis()->SetTitle(“Y Axis”);

// Draw the multi-graph
mg->Draw(“ALP”);

// Update the canvas
c->Update();
}

Yes, that’s a possibility. The first column represents the x-values, and the last three columns are different y-values that you can plot on separate graphs grouped in a multigraph.

You can also do:

graph1->AddPoint(x, y1);
graph2->AddPoint(x, y2);
graph3->AddPoint(x, y3);

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.