How to set X axis range after using SetNdivisions?

Hi everyone,

I have a TH1I histogram comprising of values in [1,2,3,4].
I used

 TH1* h_hit = new TH1I("h1", "Hist", 6, 0-0.5, 6-0.5);

and after filling

 h1->SetNdivisions(6);

so as to adjust the bin centers like the below

I am trying to set the X-axis to start from 0 (instead of -0.5) however the SetRangeUser method doesn’t seem to respond. Can anyone please help me in this regard as to how can I change the range of the X-axis to start from 0?

Thanks in advance !

Your bin starts at -0.5, and ROOT cannot hide fractions of a bin, so given your binning you have to choose either -0.5 or 0.5, but not 0.0

1 Like

Thanks a lot for pointing this out.

In the context of the ROOT analysis framework, setting the X-axis range after using SetNdivisions can be a bit tricky, especially if you’re new to ROOT like I am.
I’ve tried to set the range of the X-axis. You typically use the method SetRangeUser(min, max) on the axis object. This is assuming you’re dealing with a histogram or a graph where you can access the axis via the histogram/graph object.

I see; thanks for the insight. Yes, I am a new user of ROOT.

Can you please share some suggestions, if any, on how to set the X-axis range after using NDivisions?

https://root.cern/doc/master/classTAttAxis.html#ae3067b6d4218970d09418291cbd84084

1 Like

Thanks a lot @couet !

1 Like

Hi,

just to be clear: the TAttAxis::SetNdivisions has nothing to do with your problem. In your previous post you asked two different and independent things:

  • how to make bins centered around integers instead of half-integers;
  • how to get rid of labels with half-integers.

My reply to the first question was to shift the bins by 1/2 bin to the left, and my reply to your second question was to use the TAttAxis::SetNdivisions.
What you want to do now has only to do with shifting the bins to the left, and has nothing to do with TAttAxis::SetNdivisions. As @dastudillo pointed out, you cannot hide half of bin with TH1::SetRange or TH1::SetRangeUser. What you can do is to use a histogram with variable bin width instead, “splitting” your first bin (-0.5 < ID < +0.5) into two: -0.5 < ID < 0 and 0 < ID < +0.5. All other bins should be kept unchanged. Then you can “hide” your first bin (the one with -0.5 < ID < 0) using TH1::SetRange or TH1::SetRangeUser.

1 Like

Oh! Yeah, I got mixed up with SetNdivisions and shifting the bins.

Thanks a lot for pointing out the solution.

As I see, I’m a bit late with my reply. @yus answer is what you should do.