# How to plot error bars along with fitthing the curve in TGraph?

**URL:** <https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422>\
**Category:** Newbie\
**Created:** [November 24, 2020, 3:25pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422 "2020-11-24T15:25:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [November 24, 2020, 3:25pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/1 "2020-11-24T15:25:04Z")

</div>

_ROOT Version:_ 6.22  
_Platform:_ Ubuntu

* * *

I want to plot error bars and fit the graph along with it. The code I have written is,

```auto
{
  f = new TF1("fit", "[0]*x + [1]",0,5);
  f -> SetParNames("Slope", "Interceptor");
  double x[5]={2.68,4.73,0.415, 4.34, 4.73}, y[5] = {661.66, 1274.54,122.061, 1173.24, 1332.51}, xerr[5]= {0.119,0.10625, 0.10625, 0.204, 0.21675}, yerr[5]={0,0,0,0,0};
  g = new TGraphErrors(5, x, y, xerr, yerr);
  g -> Fit(fit, "R"); // Had also put f instead of fit and trying without "R" also but same graph showing
  g->Draw(); // Also tried with "AP" , "A", "A*" but nothing happened
}

```

But there is still a problem remains. the fitting line did not show up on the graph.  
please help me on it.

Thank you,  
Dsubhro.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2020, 4:54pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/2 "2020-11-24T16:54:53Z")

</div>

Your points along X are not ordered. The fit did not work

```auto
Warning in <Fit>: Abnormal termination of minimization.

```

the following simple TGraph fitting example works:

```auto
{
   gStyle->SetOptFit(1);
   Double_t x[] = {1., 2., 3.};
   Double_t y[] = {2., 3., 2.};
   auto g = new TGraph(3, x, y);
   g->Fit("gaus");
   g->Draw("A*");
}

```

---

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [November 24, 2020, 4:58pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/3 "2020-11-24T16:58:32Z")

</div>

Sir,  
I need to plot the error bars also along with the fitting curve. Can you please tell me how to do that??

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2020, 5:10pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/4 "2020-11-24T17:10:35Z")

</div>

Use TGraphErrors as you did before… but as I sais your point along X are not ordered and the fit did not work. Fix that and you will get the plot.

---

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [November 24, 2020, 6:01pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/5 "2020-11-24T18:01:43Z")

</div>

Sir, I have written the code like,

```auto
{
  TF1 * f = new TF1("fit", "[0]*x + [1]",0,5);
  gStyle -> SetOptFit(1);
  f -> SetParNames("Slope", "Interceptor");
  double x[5]={0.415,2.68,4.34,4.73,4.73}, y[5] = {122.061,661.66,1173.24,1274.54,1332.51}, xerr[5]= {0.10625,0.119,0.204,0.10625,0.21675}, yerr[5]={0,0,0,0,0};
  TGraphErrors *g = new TGraphErrors(5, x, y, xerr, yerr);
  g -> Fit(f);
  g->Draw("A*");
}

```

but the output:

```auto
Warning in <Fit>: Abnormal termination of minimization.
 FCN=0 FROM MIGRAD STATUS=FAILED 102 CALLS 103 TOTAL
                     EDM=44636 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
  EXT PARAMETER APPROXIMATE STEP FIRST   
  NO. NAME VALUE ERROR SIZE DERIVATIVE 
   1 Slope 0.00000e+00 5.72655e-32 0.00000e+00 -3.68935e+33
   2 Interceptor 0.00000e+00 1.41421e+00 -0.00000e+00 0.00000e+00
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1

```

and the output file: [graph.pdf](https://root-forum.cern.ch/uploads/short-url/1fJPfKTBAwEZGMnytryYLtIhKBj.pdf) (13.6 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2020, 6:13pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/6 "2020-11-24T18:13:46Z")

</div>

Notice the fit did not work. You have the message:

```auto
Warning in <Fit>: Abnormal termination of minimization.

```

May be @moneta can help.

---

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [November 24, 2020, 6:18pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/7 "2020-11-24T18:18:28Z")

</div>

Yes sir, I did notice that even after ordering the the points along X axis.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2020, 6:23pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/8 "2020-11-24T18:23:36Z")

</div>

You should also set some initial value for the parameters:

```auto
{
  TF1 * f = new TF1("fit", "[0]*x + [1]",0,5);
  gStyle -> SetOptFit(1);
  f->SetParNames("Slope", "Interceptor");
  f->SetParameter(0, 10);
  f->SetParameter(1, 10);
  double x[5]={0.415,2.68,4.34,4.73,4.73},
          y[5] = {122.061,661.66,1173.24,1274.54,1332.51},
          xerr[5]= {0.10625,0.119,0.204,0.10625,0.21675},
          yerr[5]={0,0,0,0,0};
  TGraphErrors *g = new TGraphErrors(5, x, y, xerr, yerr);
  g -> Fit(f);
  g->Draw("A*");
}

```

---

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [November 24, 2020, 7:54pm UTC](https://root-forum.cern.ch/t/how-to-plot-error-bars-along-with-fitthing-the-curve-in-tgraph/42422/9 "2020-11-24T19:54:12Z")

</div>

Thank you very much.
