Setting Log Scale on just y-axis of a TH2F graph

I am trying to make a 2-D histogram of kinetic energy and PROMPT photoelectrons, but trying to set a log scale on the y-axis. The y-axis changes to a log scale but the tick marks dont appear properly and someone told me that it is not centered at 0. I tried “correlation->GetYaxis()->SetRangeUser(1e-1, 5000);” but this did not work. An image of the graph is shown here.


Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.


    auto canvas = new TCanvas("Canvas","Canvas",1400,900);
    auto correlation = new TH2F("Kinetic-PromptPE","Kinetic-PromptPE",50,0,250,50,0,5000);
    for(size_t i = 0 ;i < promptPE.size(); i++){
        if(!kinetic[i].empty()){
            correlation->Fill(kinetic[i][kinetic[i].size()-1],promptPE[i]);
        }

    }
    canvas->Draw();
    correlation->Draw("COLZ");
    gStyle->SetOptStat(0);
    gPad->SetLogy(1);
    correlation->GetXaxis()->SetTitle("Kinetic Energy / 5 (MeV)");
    correlation->GetYaxis()->SetTitle("Prompt PE / 100");
    canvas->SaveAs("/exp/sbnd/app/users/ipatel/NC_trigger/plot/Kinetic-PromptPE.png","recreate");


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Try instead (before Drawing)

correlation->SetMinimum(1e-1);

So this only changes the minimum for the z-axis, shown in this image below. I need it on the y-axis alone, so it shows more tickmarks on the y-axis.

?? It changes the minimum of the y-axis. Which do you think are the x,y,z axes? x,y are the usual, and z is the colour scale on the right side.

I read somewhere that the SetMinimum for TH2F graph makes the z-axis(the color) minimum to 0. Also the other issue that I stated was that the tick-marks on the y-axis dont appear as they normally do for 0,10^2,then`10^3 and so on.

Hi,

Nice explanation at https://root.cern.ch/doc/v630/classTH1.html:

One can use TH1::SetMaximum() and TH1::SetMinimum() to force a particular value for the maximum or the minimum scale on the plot. (For 1-D histograms this means the y-axis, while for 2-D histograms these functions affect the z-axis).

What do mean by that? First of, 0 is impossible to show on a log-scale plot. Second, you have the 10^3 tick shown on your plot: ticks below correspond to 9*10^2, 8*10^2, 7*10^2, …, 2*10^2. The ticks above are for 2*10^3, 3*10^3, and 4*10^3. What exactly is wrong with this?

Indeed, sorry for the confusion. If what you want is to change the lowest exponent shown on the axis, try SetLimits:

 {
  auto c1    = new TCanvas("c1","c1",1500,500);
  c1->Divide(3,1);

  auto hcol1 = new TH2F("hcol1","Original",40,-4,4,40,0,700);
  float px, py;
  for (Int_t i = 0; i < 50000; i++) {
    gRandom->Rannor(px,py);
    hcol1->Fill(px,100*py);
  }

  c1->cd(1);
  hcol1->DrawCopy("COLZ");

  c1->cd(2);
  gPad->SetLogy(1);
  hcol1->SetTitle("Logy");
  hcol1->DrawCopy("COLZ");

  c1->cd(3);
  gPad->SetLogy(1);
  hcol1->SetTitle("Logy + SetLimits");
  hcol1->GetYaxis()->SetLimits(1e-1,500);
  hcol1->DrawCopy("COLZ");
}


Edit: Note, by the way, that SetLimits may not be the best option, as it can alter your histogram (see this).

My advisor just told me that the y-axis is not actually beginning at 0, I understand for a logarithmic scale you cant really have that, but somewhere close should do. Also she said that because the only labeled tickmarks that were showing were the 10^3 and none of the others. So, I was wondering how to actually set the bottom to 0 and get more labeled tickmarks.

h1col->GetYaxis()->SetMoreLogLabels();

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