# Fitting graph and specifying label options

**URL:** <https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917>\
**Category:** Newbie\
**Tags:** graphics\
**Created:** [November 22, 2019, 11:26am UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917 "2019-11-22T11:26:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![schniefen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@schniefen](https://root-forum.cern.ch/u/schniefen)\
**Post date:** [November 22, 2019, 11:26am UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/1 "2019-11-22T11:26:34Z")

</div>

Consider the attached file. When trying to fit the graph with a polynomial of degree 1 (with the range specified in the graph), the fit turns out to be quite erroneous. What could be the reason for that? The code is very similar to the code in the first example of the TGraphPainterClass. Besides, how would one fix the x-labels so that the decimals are only specified to two or three?

Thanks!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 22, 2019, 11:56am UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/2 "2019-11-22T11:56:55Z")

</div>

Try a two-steps approach:

```cpp
TF1 *f = ((TF1*)(gROOT->GetFunction("pol1")));
graph->Fit(f, "WQN", "", xmin, xmax); // "initial pre-fit"
graph->Fit(f, "", "", xmin, xmax); // "final fit"

```

---

<div class="post-metadata">

**Author:** ![schniefen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@schniefen](https://root-forum.cern.ch/u/schniefen)\
**Post date:** [November 22, 2019, 12:38pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/3 "2019-11-22T12:38:45Z")

</div>

Where would one insert this? In the terminal? The value of p1 when applying the fit yields the desired slope, but it is very large and it would be nice to get the fit fitted onto the points.

---

<div class="post-metadata">

**Author:** ![schniefen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@schniefen](https://root-forum.cern.ch/u/schniefen)\
**Post date:** [November 22, 2019, 12:42pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/4 "2019-11-22T12:42:33Z")

</div>

Is there a solution to reduce the number of decimals displayed in the x-labels? SetDecimals only adds/removes one decimal in the FitPanel. Thanks!

---

<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 22, 2019, 1:25pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/5 "2019-11-22T13:25:14Z")

</div>

> [@schniefen](#):
>
> Is there a solution to reduce the number of decimals displayed in the x-labels?

Seems to me you are specifying a negative number of divisions along the X axis.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 22, 2019, 1:34pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/6 "2019-11-22T13:34:13Z")

</div>

To me it seems that the “x range” is very small, from xleft = 5.6606978352e-6 to xright = 5.660697837e-6

---

<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 22, 2019, 1:52pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/7 "2019-11-22T13:52:57Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> To me it seems that the “x range” is very smal

Yes but the ticks look not optimized…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 22, 2019, 2:08pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/9 "2019-11-22T14:08:08Z")

</div>

Try with xmin = 5.6606978352e-6 and xmax = 5.6606978365e-6

---

<div class="post-metadata">

**Author:** ![schniefen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@schniefen](https://root-forum.cern.ch/u/schniefen)\
**Post date:** [November 22, 2019, 2:41pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/10 "2019-11-22T14:41:30Z")

</div>

Changing the divisions in the Editor to match the xmin and xmax values won’t change anything. Do the divisions need to be fixed in the macro beforehand?

---

<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 22, 2019, 2:54pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/11 "2019-11-22T14:54:56Z")

</div>

Axis labels vary at the 9th decimal. that’s why they cannot be optimized.  
the difference between x min and x max is 1.8000001e-09  
 ![05](https://root-forum.cern.ch/uploads/default/original/3X/3/f/3f334d93f87ac6f04448586d352802212b25afc2.jpeg)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 22, 2019, 3:38pm UTC](https://root-forum.cern.ch/t/fitting-graph-and-specifying-label-options/36917/12 "2019-11-22T15:38:33Z")

</div>

```auto
gr->Sort(); // make sure points are in the "increasing order in x"
Int_t ifirst = 0, ilast = 200;
Double_t p1 = (gr->GetY()[ilast] - gr->GetY()[ifirst]) / (gr->GetX()[ilast] - gr->GetX()[ifirst]);
Double_t p0 = gr->GetY()[ifirst] - p1 * gr->GetX()[ifirst];
// std::cout << p0 << " " << p1 << std::endl;
TF1 *f = ((TF1*)(gROOT->GetFunction("pol1")));
f->SetParameters(p0, p1); // set "reasonable" initial values
gr->Fit(f, "F", "", gr->GetX()[ifirst], gr->GetX()[ilast]);

```
