Home | News | Documentation | Download

How to set logarithmic scale on y-axis in a TGraphError script

Hi,
I am plotting the TGraphError with a logarithmic scale on the y-axis by using the attached script and txt file below.

But it is not accepting log scale neither through script nor manually through Draw Panel. Also, it picks the error bars of only graph2 not of graph1.

Can you please help me to debug this issue?

Regards & Thanks
read_graph_txt_AMPT_data.C (1.1 KB)

ALICE.txt (203 Bytes)
data.txt (3.3 KB)

Try with:

    auto c = new TCanvas();
    c->SetGridy();
    c->SetLogy();
    ...

No, it’s not working.
Showing the same error of ‘Cannot set Y-axis to log scale’ with a blank file.

OK, since it works for me (see below) maybe @couet has an idea…

For me this works:

// Reads the points from a txt file and produces a simple comparison graph.
void read_graph_txt_AMPT_data()
{
   auto c=new TCanvas();
   c->SetGridy();
   c->SetLogy();

   TGraphErrors graph1("./data.txt","%lg %lg %lg 0");     // * means not select this column of data
   TGraphErrors graph2("./ALICE.txt","%lg %lg %lg 0");     // * means not select this column of data

   graph1.SetMarkerStyle(20);
   graph1.SetMarkerColor(kBlue);

   graph2.SetMarkerStyle(20);
   graph2.SetMarkerColor(kRed);

   TMultiGraph mg;
   mg.Add(&graph1);
   mg.Add(&graph2);
   mg.GetXaxis()->SetTitle("p_{T}(GeV)");
   mg.GetYaxis()->SetTitle("dN/dp_{T}(GeV^{2})");
   mg.GetXaxis()->CenterTitle();
   mg.GetYaxis()->CenterTitle();

   mg.DrawClone("ALP");
}

1 Like

Thank you, @couet but it’s not working for me. Then I think there is some issue with my root version(root_v6.20.06.source) or something else.

Did you try to run the .C macro I sent you ?
Yes, if that simple macro does not work for you, it might be an issue with the ROOT version. By the way on which operating system are you working ?
By the way, if I do not set the log scale in the macro I sent you, I get the following plot. Do you get the same in linear scale ?

Yes. I did try to run the .C macro which you sent me. It is not working.

My operating system Ubuntu-20.04 and ROOT version is 6.

No also without log scale I got different plot from you. Here I am attaching the plot on linear scale which is very different.

linear plot.pdf (18.5 KB)

Ah yes sorry a file data.txt already exists in my Download directory. The wrong one was use. Here is the plots I get with your file.


Ooooh… Do you have any suggestions on what should I do?

Which ROOT version are you using exactly ? I do not have more suggestions All is fine here with le most recent ROOT version.

My root version is root_v6.20.06.source.

Ok, I installed an older ROOT (6.20 patches) on my machine and I get the same problem: blank canvas and an error message:

% root 
root [0] .x read_graph_txt_AMPT_data.C
Error in <THistPainter::PaintInit>: log scale requested with a negative argument (-0.151850)
root [1] 

So to fix this problem you need to use a more recent ROOT version.

Thanks, @couet for your kind help.
root_v6.24.06 Is this version okay to fix this problem?

no…

% root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.24/07                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Jan 13 2022, 13:46:14                      |
  | From heads/v6-24-00-patches@v6-24-06-16-g3b59acd110              |
  | With Apple clang version 12.0.5 (clang-1205.0.22.9)              |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] .x read_graph_txt_AMPT_data.C
Error in <THistPainter::PaintInit>: log scale requested with a negative argument (-0.151850)
root [1] 

I guess 6.26 or master … For me it is working with master…

I found the PR which fixed this issue.

To make it work with the version you have you can add:

   mg.SetMinimum(0.001);

before DrawClone

Thank you so much @couet
Now log scale working very well.
But when I am setting the Y-axis range in the .C macro script it is not accepting the range.
And another problem I am facing is that in the range of approx 20 - 40 GeV of X-axis pT range, it shows double or triangle type error bar(attached pdf).

I will be very thankful for your kind assistance if you have a look at this.

read_graph_txt_AMPT_data.C (937 Bytes)
ALICE.txt (170 Bytes)
data.txt (2.7 KB)
error graph.pdf (21.8 KB)

well, if you specify a valid positive range then you should remove SetMinimum()

// Reads the points from a txt file and produces a simple comparison graph.
void read_graph_txt_AMPT_data()
{
   auto c=new TCanvas();
   //c->SetGridy();
   c->SetLogy();

   TGraphErrors graph1("./data.txt","%lg %lg %lg 0");     // * means not select this column of data
   TGraphErrors graph2("./ALICE.txt","%lg %lg %lg 0");     // * means not select this column of data

   graph1.SetMarkerStyle(20);
   graph1.SetMarkerColor(kBlue);
   //graph1.GetYaxis()->SetRangeUser(10e-10, 4);

   graph2.SetMarkerStyle(20);
   graph2.SetMarkerColor(kRed);
   //graph2.GetYaxis()->SetRangeUser(10e-10, 4);

   TMultiGraph mg;
   mg.Add(&graph1);
   mg.Add(&graph2);
   mg.GetXaxis()->SetTitle("p_{T}(GeV)");
   mg.GetYaxis()->SetTitle("dN/dp_{T}(GeV^{2})");
   mg.GetXaxis()->CenterTitle();
   mg.GetYaxis()->CenterTitle();
   mg.GetYaxis()->SetRangeUser(10e-10, 4);
   mg.DrawClone("ALP");
}

Thank you so much it’s working.