# Loading data file to make a plot of multiple y values for a single x values

**URL:** <https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589>\
**Category:** General Chat\
**Created:** [July 25, 2016, 2:03pm UTC](https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589 "2016-07-25T14:03:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpoudel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b5ac83/32.png) [@mpoudel](https://root-forum.cern.ch/u/mpoudel)\
**Post date:** [July 25, 2016, 2:03pm UTC](https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589/1 "2016-07-25T14:03:42Z")

</div>

Hi  
I have 4 .dat files each with 2 arrays. In each files, First one array corresponds to Voltage and second one correspond to Gain at different conditions. I want to make a plot of Gain vs. Voltage at one graph (not individually).  
Can any one help me out with a code?  
I wrote the following one but it doesnt work.

{

```
TCanvas *c1= new TCanvas ("c1", "", 700, 600);

TMultiGraph *mg= new TMultiGraph ();

TGraph gr0 ("CH0.dat", "%lg %lg");
gr0.SetMarkerStyle (kFullCircle);	
gr0.SetMarkerColor (kRed);	
gr0.Draw ("");

TGraph gr1 ("CH1.dat", "%lg %lg");
gr1.SetMarkerStyle (kFullCircle);
gr1.SetMarkerColor (kBlue);
gr1.Draw("");

TGraph gr2 ("CH2.dat", "%lg %lg");
gr2.SetMarkerStyle (kMultiply);
gr2.SetMarkerColor (kBlack);
gr2.Draw("");

TGraph gr3 ("CH3.dat", "%lg %lg");
gr3.SetMarkerStyle (kStar);
gr3.SetMarkerColor (kBlack);
gr3.Draw ("");

mg->Add ("gr0");
mg->Add ("gr1");
mg->Add ("gr2");
mg->Add ("gr3");

mg->Draw("");

```

}

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [July 25, 2016, 3:18pm UTC](https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589/2 "2016-07-25T15:18:45Z")

</div>

[code]{  
TGraph \*gr0 = new TGraph (“CH0.dat”, “%lg %lg”);  
gr0-\>SetMarkerStyle (kFullCircle);  
gr0-\>SetMarkerColor (kRed);

TGraph \*gr1 = new TGraph (“CH1.dat”, “%lg %lg”);  
gr1-\>SetMarkerStyle (kFullCircle);  
gr1-\>SetMarkerColor (kBlue);

TGraph \*gr2 = new TGraph (“CH2.dat”, “%lg %lg”);  
gr2-\>SetMarkerStyle (kMultiply);  
gr2-\>SetMarkerColor (kBlack);

TGraph \*gr3 = new TGraph (“CH3.dat”, “%lg %lg”);  
gr3-\>SetMarkerStyle (kStar);  
gr3-\>SetMarkerColor (kBlack);

TMultiGraph \*mg = new TMultiGraph();  
mg-\>Add (gr0);  
mg-\>Add (gr1);  
mg-\>Add (gr2);  
mg-\>Add (gr3);

TCanvas \*c1 = new TCanvas (“c1”, “”, 700, 600);

mg-\>Draw(“AP”);  
}[/code]

---

<div class="post-metadata">

**Author:** ![mpoudel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b5ac83/32.png) [@mpoudel](https://root-forum.cern.ch/u/mpoudel)\
**Post date:** [July 26, 2016, 7:30am UTC](https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589/3 "2016-07-26T07:30:32Z")

</div>

Thank you so much. It worked. ![8)](https://root-forum.cern.ch/uploads/default/original/1X/fe5f42ae79e389360ae9d969db4dcc4bca03d384.gif "Cool")  
However I am not able to set colors of my choice for pol1 fit lines. i used the following code: [code]{  
TGraph \*gr0 = new TGraph (“CH0.dat”, “%lg %lg”);  
gr0-\>SetMarkerStyle (kFullCircle);  
gr0-\>SetMarkerColor (kRed);  
gr0-\>Fit(“pol1”);  
gr0-\>SetLineColor(kRed);

TGraph \*gr1 = new TGraph (“CH1.dat”, “%lg %lg”);  
gr1-\>SetMarkerStyle (kFullCircle);  
gr1-\>SetMarkerColor (kBlue);  
gr1-\>Fit(“pol1”);  
gr1-\>SetLineColor (kBlue);

TGraph \*gr2 = new TGraph (“CH2.dat”, “%lg %lg”);  
gr2-\>SetMarkerStyle (kMultiply);  
gr2-\>SetMarkerColor (kBlack);  
gr2-\>Fit(“pol1”);  
gr2-\>SetLineColor (kBlack);

TGraph \*gr3 = new TGraph (“CH3.dat”, “%lg %lg”);  
gr3-\>SetMarkerStyle (kStar);  
gr3-\>SetMarkerColor (kGreen);  
gr3-\>Fit(“pol1”);  
gr3-\>SetLineColor (kGreen);

TMultiGraph \*mg = new TMultiGraph();  
mg-\>SetTitle (“Gain vs. Voltage”);  
mg-\>Add (gr0);  
mg-\>Add (gr1);  
mg-\>Add (gr2);  
mg-\>Add (gr3);

TCanvas \*c1 = new TCanvas (“c1”, “”, 700, 600);

mg-\>Draw(“AP”);  
}  
[/code]

Also, before whenever I wrote  
TGraph \*gr0 = new TGraph ()  
gr0-\>Draw ()

an error message used to pop up saying TGraph doesnt belong to POINTER class and I should use Dots (.) instead… But right now its working with Pointers… Could you please explain me why?

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [July 26, 2016, 3:19pm UTC](https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589/4 "2016-07-26T15:19:50Z")

</div>

`// ...
gr0->GetFunction("pol1")->SetLineColor(kRed);
// ...
gr1->GetFunction("pol1")->SetLineColor(kBlue);
// ...
gr2->GetFunction("pol1")->SetLineColor(kBlack);
// ...
gr3->GetFunction("pol1")->SetLineColor(kGreen);
// ...`

---

<div class="post-metadata">

**Author:** ![mpoudel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b5ac83/32.png) [@mpoudel](https://root-forum.cern.ch/u/mpoudel)\
**Post date:** [July 28, 2016, 9:40pm UTC](https://root-forum.cern.ch/t/loading-data-file-to-make-a-plot-of-multiple-y-values-for-a-single-x-values/21589/5 "2016-07-28T21:40:00Z")

</div>

Thank you so much. It worked now.
