TH2 fill with binned data then change axis limits

Hi I wonder if someone can help me.
I am trying to do the following:

  • Fill a TH2 with binned data (pre-defined axis limits and bins) with SetBinContent()
  • Draw the histogram on a new axis with extended axis range.
    So I try this:
kernel_hist = TH2F("kernel", "Kernel PDF", 100, phsp.lowerLimit(1), phsp.upperLimit(1),
                                           100, phsp.lowerLimit(2), phsp.upperLimit(2))
for i in range(100):
    for j in range(100):
        kernel_hist.SetBinContent(i, j, hist2d[i,j])

then:

kernel_hist.Draw("zcol")
kernel_hist.GetXaxis().SetTitle("M^{2}(K_{S}#pi), GeV")
kernel_hist.GetYaxis().SetTitle("M^{2}(#pi#pi), GeV")
kernel_hist.GetXaxis().SetLimits(0,3.5)
kernel_hist.GetYaxis().SetLimits(0,2.0)

What happens is the histogram is stretched along with the axes.
Is there a way of changing the axes without changing the histogram?

ROOT Version: 6.18/04


Well, “SetLimits” changes the full range which was set at histogram’s creation time.
Maybe you actually want:

kernel_hist.GetXaxis().SetRangeUser(0., 3.5)
kernel_hist.GetYaxis().SetRangeUser(0., 2.0)

Hi so when I try SetRangeUser it seems to have no effect and the axes have the original limits. This has been mentioned in previous questions on the forum but I can’t find a solution. Any other ideas?

Many thanks for your response.

Maybe you should first modify the axes (“SetLimits” or “SetRangeUser”) and only then call “Draw”.

I’m afraid this did not work either.
The solution I found was to redefine the bins to leave some empty bins on either side of my histogram. This is a bit of a work-around though since the binning was predefined so had to add bins of a certain size and leave the content as zero in order to get a wider axis range.

May be you can also draw first the “frame” with the limits you needs using gPad->DrawFrame() and then draw your histogram using option SAME.

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