Problem with log scale on TGraphErrors

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


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.

Do you have a small reproducer ?

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:

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 (13.8 KB)

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

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");
}

@couet It sounds like a bug to me. :thinking:

I made a simpler version:

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.

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!

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.

1 Like