# Problem with log scale on TGraphErrors

**URL:** <https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497>\
**Category:** Newbie\
**Created:** [October 24, 2019, 5:51pm UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497 "2019-10-24T17:51:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ella](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/cdc98d/32.png) [@Ella](https://root-forum.cern.ch/u/Ella)\
**Post date:** [October 24, 2019, 5:51pm UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/1 "2019-10-24T17:51:38Z")

</div>

Hi,  
I’m trying to set the log scale for both the X and Y axis of a TCanvas where i want to plot a TGraphErrors and perform a linear Fit. I tried to use both the global gPad-\>SetLogy() and gPad-\>SetLogx() but it doesn’t work: on the Y axis the log scale is set properly but on the X axis everything disappears, and also, the fit that is supposed to be linear is not.  
Do you have any suggestions about how to properly set the log scales in this situation?

Thank you very much

* * *

\_ROOT Version: 6.19/01  
\_Platform: Linux

* * *

---

<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:** [October 24, 2019, 6:03pm UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/2 "2019-10-24T18:03:50Z")

</div>

Are you sure that all your “X” values are positive?

Note that a “linear function” usually does not look as a “straight line” when drawn in a [log-log plot](https://en.wikipedia.org/wiki/Log%E2%80%93log_plot).

---

<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:** [October 25, 2019, 6:49am UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/3 "2019-10-25T06:49:58Z")

</div>

Do you have a small reproducer ?

---

<div class="post-metadata">

**Author:** ![Ella](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/cdc98d/32.png) [@Ella](https://root-forum.cern.ch/u/Ella)\
**Post date:** [October 25, 2019, 8:09am UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/4 "2019-10-25T08:09:37Z")

</div>

Thank you very much for the answers.  
Yes I’m sure all the x values are positive, here it is the code to reproduce my problem:

```auto
void vth2(){
  
  Double_t th[3];
  th[0]=30.0;
  th[1]=70.0;
  th[2]=100.0;
  
  Double_t fl[3];
  fl[0]=2075.0;
  fl[1]=2160.0;
  fl[2]=2205.0;
  
  Double_t efl[3];
  efl[0]=15.0;
  efl[1]=15.0;
  efl[2]=15.0;
  
  
  TGraphErrors* gr1 = new TGraphErrors(3, fl, th, 0, efl);
  
  
  //TF1* f1 = new TF1("f1", "pol1(0)", 2000, 2300.00, 2);
  
  TCanvas* c1 = new TCanvas("c1", "c1", 0, 0, 500, 500);
  
  gStyle->SetOptFit(kTRUE);
  
  c1->cd();
  gPad->SetLogx();
  gPad->SetLogy();
  gr1->Fit("pol1");
  gr1->Draw("AP");
  c1->Clear();
  gr1->Draw("AP");
   
   

}

```

and from the picture you can see what i get as a result  
[graph.pdf](https://root-forum.cern.ch/uploads/short-url/bQXaWkECYX006SqdZhELPybacrn.pdf) (13.8 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:** [October 25, 2019, 8:37am UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/5 "2019-10-25T08:37:05Z")

</div>

It is because you have less than a decade along X. I would recommend to do:

```auto
void vth2(){
   Double_t th[3];
   th[0]=30.0;
   th[1]=70.0;
   th[2]=100.0;

   Double_t fl[3];
   fl[0]=2075.0;
   fl[1]=2160.0;
   fl[2]=2205.0;

   Double_t efl[3];
   efl[0]=15.0;
   efl[1]=15.0;
   efl[2]=15.0;

   TGraphErrors* gr1 = new TGraphErrors(3, fl, th, 0, efl);

   TCanvas* c1 = new TCanvas("c1", "c1", 0, 0, 500, 500);

   gStyle->SetOptFit(kTRUE);

   gPad->SetLogy();
   gr1->Fit("pol1");
   gr1->Draw("AP");
}

```

---

<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:** [October 25, 2019, 9:20am UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/6 "2019-10-25T09:20:47Z")

</div>

> [@couet](#):
>
> It is because you have less than a decade along X.

@couet It sounds like a bug to me. 🤔

---

<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:** [October 25, 2019, 11:21am UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/7 "2019-10-25T11:21:23Z")

</div>

I made a simpler version:

```auto
void vth2(){
   Double_t th[3];
   th[0]=30.0;
   th[1]=70.0;
   th[2]=100.0;

   Double_t fl[3];
   fl[0]=2075.0;
   fl[1]=2160.0;
   fl[2]=3205.0;

   auto gr1 = new TGraph(3, fl, th);
   gr1->Draw("A*L");

   gPad->SetLogx();
   gPad->SetLogy();
}

```

The points on X axis are in the 1000-10000 decade. The ticks in that decade are on 1000, 2000, 3000, 4000, 5000, 6000 7000, 8000, 9000 and 10000

The X values of this graph are 2075, 2160, and 2205. so between two ticks 2000 and 3000.

---

<div class="post-metadata">

**Author:** ![Ella](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/cdc98d/32.png) [@Ella](https://root-forum.cern.ch/u/Ella)\
**Post date:** [October 25, 2019, 9:42pm UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/8 "2019-10-25T21:42:05Z")

</div>

Unfortunately I need a log-log scale in order to perform a linear fit and thus estimating a parameter that is the slope;  
Probably I’ll have to try a different approach with ROOT because of this “bug”.

Thank you anyways for the help!

---

<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:** [October 26, 2019, 5:13am UTC](https://root-forum.cern.ch/t/problem-with-log-scale-on-tgrapherrors/36497/9 "2019-10-26T05:13:01Z")

</div>

These “scales” are just for drawing / displaying it. They have nothing to do with the fitting. You will get the same “parameters” from the fit, regardless how you draw it.

Your graph seems to look fine when one draws it in a simple linear-linear plot.
