# Draw fit function with its errors

**URL:** <https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686>\
**Category:** Newbie\
**Created:** [July 28, 2020, 3:43am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686 "2020-07-28T03:43:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 28, 2020, 3:43am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/1 "2020-07-28T03:43:02Z")

</div>

I have a graph with its errors. I create a fit function for that graph. And I want to draw fit function with its errors. Could you help me, please?

```auto
#include <TGraphErrors.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TVirtualFitter.h>
void alo()
{
   TCanvas *c = new TCanvas("c","alo",700,500);
   c->SetGrid();
   
   TGraphErrors *g = new TGraphErrors("cm0.txt");
   g->Draw("AP");
   g->SetMarkerStyle(8);

   TF1 *fit =new TF1("fit", "exp([0] + [1]*log(x) + [2]*((log(x))^2) + [3]*((log(x))^3))", 0 , 2000);
   g->Fit("fit");

   TVirtualFitter::GetFitter()->GetConfidenceIntervals(g, 0.95); 
}

```

Here is my code and plot. But the uncertainty of fit function wasn’t draw.

 ![Screenshot_1](https://root-forum.cern.ch/uploads/default/original/3X/9/1/91a381b6937fef3bb08c08862c40348470a0ab33.png)

---

<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:** [July 28, 2020, 7:14am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/2 "2020-07-28T07:14:45Z")

</div>

> [@hammiu](#):
>
> And I want to draw fit function with its errors

it looks like that’s what you get ? Am I wrong ?

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [July 28, 2020, 7:48am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/3 "2020-07-28T07:48:39Z")

</div>

I guess @hammiu would like to plot the errors of the fit as a band (something like [here](https://micropore.files.wordpress.com/2017/02/fit1.png)).

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 28, 2020, 2:40pm UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/4 "2020-07-28T14:40:43Z")

</div>

Yes, I want to draw a band of fit function.

> [@pamputt](#):
>
> I guess @hammiu would like to plot the errors of the fit as a band (something like [here](https://micropore.files.wordpress.com/2017/02/fit1.png)).

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 28, 2020, 2:42pm UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/5 "2020-07-28T14:42:30Z")

</div>

I want to draw a band of fit function (like [https://micropore.files.wordpress.com/2017/02/fit1.png](https://micropore.files.wordpress.com/2017/02/fit1.png)

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 28, 2020, 2:46pm UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/6 "2020-07-28T14:46:56Z")

</div>

Hi @hammiu,  
This [tutorial](https://root.cern/doc/master/ConfidenceIntervals_8C.html) may help you with that. Looking at the code of your first post it may be that all you need is to first call `TVirtualFitter::GetFitter()->GetConfidenceIntervals` and only call `Draw` at the end.  
Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 29, 2020, 9:10am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/7 "2020-07-29T09:10:19Z")

</div>

I don’t know exactly command to draw the band.

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 29, 2020, 9:12am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/8 "2020-07-29T09:12:06Z")

</div>

> [@couet](#):
>
> > [@hammiu](#):
> >
> > And I want to draw fit function with its errors
> 
> it looks like that’s what you get ? Am I wrong ?

Could you help me, please? I want to draw a band of fit function (like [https://micropore.files.wordpress.com/2017/02/fit1.png](https://micropore.files.wordpress.com/2017/02/fit1.png)

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 29, 2020, 9:15am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/9 "2020-07-29T09:15:06Z")

</div>

Sorry, what I meant is a slight modification of your initial code

```auto
#include <TGraphErrors.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TVirtualFitter.h>
void alo()
{
   TCanvas *c = new TCanvas("c","alo",700,500);
   c->SetGrid();
   
   TGraphErrors *g = new TGraphErrors("cm0.txt");

   TF1 *fit =new TF1("fit", "exp([0] + [1]*log(x) + [2]*((log(x))^2) + [3]*((log(x))^3))", 0 , 2000);
   g->Fit("fit");

   TVirtualFitter::GetFitter()->GetConfidenceIntervals(g, 0.95); 

   g->Draw("AP");
   g->SetMarkerStyle(8);
}

```

Note the call to `Draw` only at the end.  
But again, if that’s not sufficient start from the example I linked and work your way through by adding your data and custom functions.  
Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 29, 2020, 1:02pm UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/10 "2020-07-29T13:02:15Z")

</div>

Thank you, but the result is same, not confidence band.  
Here is my file. You can try it again.  
[cm0.txt](https://root-forum.cern.ch/uploads/short-url/sjxGzof4cLQ07pASBHh7ecGzFQL.txt) (456 Bytes)

---

<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:** [July 29, 2020, 2:32pm UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/11 "2020-07-29T14:32:34Z")

</div>

may be;

```auto
void alo()
{
   TCanvas *c = new TCanvas("c","alo",700,500);
   c->SetGrid();

   TGraphErrors *g = new TGraphErrors("cm0.txt");

   TF1 *fit =new TF1("fit", "exp([0] + [1]*log(x) + [2]*((log(x))^2) + [3]*((log(x))^3))", 0 , 2000);
   g->Fit("fit");

   TVirtualFitter::GetFitter()->GetConfidenceIntervals(g, 0.95);
   g->SetFillColor(kBlue);
   g->Draw("A E3");
   g->SetMarkerStyle(8);
   g->Draw("P");
}

```

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [July 30, 2020, 5:39am UTC](https://root-forum.cern.ch/t/draw-fit-function-with-its-errors/40686/12 "2020-07-30T05:39:57Z")

</div>

Thank you so much for your guidance. It work properly.
