Making second Y Axis in Logarithmic

When you are using TGaxis directly you invoke the low level axis painter which does not know about the Log option set at the pad level. To draw a log axis using TGaxis you need to use the option G as explain in the TGaxis documentation.

As suggested in the document

I replaced my TGaxis command

by the following
TF1 *f3=new TF1(“f3”,“log10(x)”,1,1000);
TGaxis *axis = new TGaxis(30,0,10000000,0,“f3”,80506,“G”);
overlay->SetLogy(1);

It gives an error:
Error in THistPainter::PaintInit: Cannot set Y axis to log scale
And my second graph is not plotted at all

I am attaching two jpeg files. One is with the above mentioned replaced TGaxis command (Only with one graph)
Second is with my previous TGaxis command ( the one having two graphs)

Also why am I seeing two almost overlapping lines in the graph which is very left side of the 2nd image. The peak seems to be having two overlapping lines.

Can you provide the file cross_section.dat ?

cross_section.dat.tar (3 KB)

This is my program:

root4.c (2.0 KB)

This is the data for second graph which I want in log scale:

phflx.dat.tar (3.5 KB)

Ok thanks. looking at it.

{
   auto c1 = new TCanvas("c1","cross-section with photon flux",1500,500);
   auto pad = new TPad("pad","",0,0,1,1);
   pad->SetGrid();
   pad->Draw();
   pad->cd();

   // draw a frame to define the range
   TH1F *hr = c1->DrawFrame(0,0,30,170);
   hr->SetXTitle("photon energy (MeV)");
   hr->SetYTitle("Microscopic cross-section (mb)");
   pad->GetFrame()->SetFillColor(21);
   pad->GetFrame()->SetBorderSize(12);

   // create first graph
   auto g1 = new TGraph("cross_section.dat");
   g1->SetMarkerStyle(5);
   g1->Draw ("PL");

   //create a transparent pad drawn on top of the main pad
   c1->cd();
   auto overlay = new TPad("overlay","",0,0,1,1);
   overlay->SetFillStyle(4000);
   overlay->SetFillColor(0);
   overlay->SetFrameFillStyle(4000);
   overlay->Draw();
   overlay->cd();

   //Create the second graph
   auto g2= new TGraph("phflx.dat");
   g2->SetMarkerColor(kRed);
   g2->SetMarkerStyle(5);
   g2->SetName("g2");
   TH1F *hframe = overlay->DrawFrame(0,0,30,10000000);
   hframe->GetXaxis()->SetLabelOffset(99);
   hframe->GetYaxis()->SetLabelOffset(99);
   g2->Draw("PL");

   //Draw an axis on the right side
   TGaxis *axis = new TGaxis(30,0,30, 10000000,0.001,10000000,510,"G+");
   axis->SetLineColor(kRed);
   axis->SetLabelColor(kRed);
   axis->SetLabelOffset(0.015);
   axis->Draw();
}
1 Like

I am attaching the log scale I get through Excel and Also attaching what I am getting through the code which you gave me. I am not getting the kind of graph needed. I am attaching what I need exactly also what I am getting through your codes.

I want the second graph (which I got through excel) over the first cross-section graph.

Thank you!

You need only to make the log axis start at 100 … I put 0.001 just to have something non zero

Yes that I could do. but you see the shape of the graph should accordingly change once you change the linear axis to log axis. That isn’t happening here. Which mean the graph points are not being plotted properly. as you see (in the attached image) The peak on the left is way lower than it shold. In the file phflx.dat which I sent you the points are as higher as 10^6 where as in the graph which I am getting the peak is even below 10^5. The shape is also not logarithmic.

The red axis is purely graphics and does not affect the graph itself. You should plot the graph in log scale by setting the log option for the pad in which the graph is drawn.

That is what I initially thought and used the following command
overlay->SetLogy(1);
But it says Y Axiis cannot be set into log. I reckon it is because I am putting two Y-Axes. But again both the Y-Axes are in different pads. So It shouldn’t be a problem. I am stuck from past two days on the same thing.

{
   auto c1 = new TCanvas("c1","cross-section with photon flux",1500,500);
   auto pad = new TPad("pad","",0,0,1,1);
   pad->SetGrid();
   pad->Draw();
   pad->cd();

   // draw a frame to define the range
   TH1F *hr = c1->DrawFrame(0,0,30,170);
   hr->SetXTitle("photon energy (MeV)");
   hr->SetYTitle("Microscopic cross-section (mb)");
   pad->GetFrame()->SetFillColor(21);
   pad->GetFrame()->SetBorderSize(12);

   // create first graph
   auto g1 = new TGraph("cross_section.dat");
   g1->SetMarkerStyle(5);
   g1->Draw ("PL");

   //create a transparent pad drawn on top of the main pad
   c1->cd();
   auto overlay = new TPad("overlay","",0,0,1,1);
   overlay->SetFillStyle(4000);
   overlay->SetFillColor(0);
   overlay->SetFrameFillStyle(4000);
   overlay->Draw();
   overlay->cd();

   //Create the second graph
   auto g2= new TGraph("phflx.dat");
   g2->SetMarkerColor(kRed);
   g2->SetMarkerStyle(5);
   g2->SetName("g2");
   TH1F *hframe = overlay->DrawFrame(0,100,30,10000000);
   hframe->GetXaxis()->SetLabelOffset(99);
   hframe->GetYaxis()->SetLabelOffset(99);
   hframe->GetYaxis()->SetTickSize(0);
   g2->Draw("PL");
   overlay->SetLogy(1);

   //Draw an axis on the right side
   TGaxis *axis = new TGaxis(30,0,30, 10000000,100,10000000,510,"G+");
   axis->SetLineColor(kRed);
   axis->SetLabelColor(kRed);
   axis->SetLabelOffset(0.02);
   axis->Draw();
}

1 Like

Thank you so much!! I think the position of SetLogy(1) command was wrong by me. Thank you for your so much help. :slight_smile::):smiley::+1:

not only … the overlay Y scale was wrong too.

Yes. I tried for the first time and I took the commands in bits and pieces from various suggestions online (some I didn’t even completely understand) that created the confusion.

I would be glad if you could tell me Why was I getting the double peaks (on left end) slightly mismatched, in my previous graph?

I am not sure I understand what you are talking about…

Oh! Nevermind. I am glad It’s working perfectly now. Thank you Again. :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.