Plot histogram with logarithmic scale

Dear ROOT team,
I would like to use a logarithmic x scale for my histograms. However, I observe that this works only for histograms starting at 0 or 1, but not at larger numbers. When I run the code below I do not get any histogram drawn, except if I set the lowest bin edge of the histogram to 0.

Is there any solution to this problem?

Please find below a minimal example:

import array, ROOT
ROOT.gROOT.SetBatch()
nbinsx = 3
xbins = array.array("d",[7,50,100,1000])
h = ROOT.TH1F("data","",nbinsx,xbins)
h.SetBinContent(1,20)
h.SetBinContent(2,50)
h.SetBinContent(3,30)
canvas  = ROOT.TCanvas( "canvas", "canvas" )
pad = ROOT.TPad("pad","pad",0.0,0.0,1.0,1.0)
pad.Draw()
pad.SetLogx(1)
pad.cd()
hTemp = h.Clone()                                                                                       
hTemp.Draw("AXIS")                                                                                      
h.Draw("same,ep")
canvas.SaveAs("test.pdf")

In fact, it does not matter for the problem if I use hTemp or not, the behaviour is the same. However, I would like to have a template histogram which I can use to set the ranges.

Thank you very much in advance!

Cheers,
Alexander


Please read tips for efficient and successful posting and posting code

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


Try adding canvas.Draw() after you create it.

{
   int nbinsx = 3;
   float xbins[4] = {7,50,100,1000};
   auto h = new TH1F("data","",nbinsx,xbins);
   h->SetBinContent(1,20);
   h->SetBinContent(2,50);
   h->SetBinContent(3,30);
   auto canvas  = new TCanvas( "canvas", "canvas" );
   canvas->SetLogx(1);
   h->Draw("ep");
}

gives me:

What is wrong ?

Dear couet,

Thank you for your answer. Unfortunately, this does not work for me:

import array, ROOT
nbinsx = 3
xbins = array.array(“d”,[7,50,100,1000])
h = ROOT.TH1F(“data”,"",nbinsx,xbins)
h.SetBinContent(1,20)
h.SetBinContent(2,50)
h.SetBinContent(3,30)

canvas = ROOT.TCanvas( “canvas”, “canvas” )
canvas.SetLogx(1)
h.Draw(“ep”)
canvas.SaveAs(“test.pdf”)

gives me just the axis without any bin contents…
Also copying your code into root itself gives the same output…

I use following ROOT version:

| Welcome to ROOT 6.20/04 https://root.cern |
| © 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for linuxx8664gcc on Apr 01 2020, 08:28:48 |
| From tags/v6-20-04@v6-20-04 |

Try ‘.help’, ‘.demo’, ‘.license’, ‘.credits’, ‘.quit’/’.q’

I put the code I sent you in a .C file… a.C and did:

root a.C

And I got the image I sent you.

When I do the same, I get just empty axes… Which ROOT version do you use?

That’s super simple it should work on any root version. I am using for instance 6.20/05 on Mac.
Can you post here the a.C file you have ?

This seems to be a bug in the ROOT 6.20/04 “tag” which has been fixed in the “patches” branch.

Thank you very much! This was the problem… With a newer version (6.22.99) it runs!

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