# How to set X axis range after using SetNdivisions?

**URL:** <https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [December 19, 2023, 11:26am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597 "2023-12-19T11:26:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [December 19, 2023, 11:26am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/1 "2023-12-19T11:26:58Z")

</div>

Hi everyone,

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

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

```

and after filling

```auto
 h1->SetNdivisions(6);

```

so as to adjust the bin centers like the below

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/a/f/af134ad5ec23b001a0b70e82d6a0f24e0142cb7a.png)

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 !

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [December 19, 2023, 11:53am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/2 "2023-12-19T11:53:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [December 19, 2023, 11:59am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/3 "2023-12-19T11:59:50Z")

</div>

Thanks a lot for pointing this out.

---

<div class="post-metadata">

**Author:** ![SapphireShade](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sapphireshade/32/35327_2.png) [@SapphireShade](https://root-forum.cern.ch/u/SapphireShade)\
**Post date:** [December 20, 2023, 8:53am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/4 "2023-12-20T08:53:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [December 20, 2023, 9:12am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/5 "2023-12-20T09:12:06Z")

</div>

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

> [@SapphireShade](#):
>
> I’ve tried to set the range of the X-axis. You typically use the method SetRangeUser(min, max) on the axis object.

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 20, 2023, 9:36am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/6 "2023-12-20T09:36:02Z")

</div>

> [@How to set ranges on axis?](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254):
>
> The viewing range for a histogram axis can be set from bin ifirst to ilast using the method TAxis::SetRange(ifirst, ilast). Example (hpxpy is a 2D histogram): { TFile f("hsimple.root"); hpxpy-\>Draw("colz"); hpxpy-\>GetYaxis()-\>SetRange(22,23); hpxpy-\>GetXaxis()-\>SetRange(18,19); } Sometimes it is more convenient to set this range using “User’s Coordinates” (axis values) rather than bin numbers. To do that one should use TAxis::SetRangeUser(ufirst, ulast). This method computes the cl…

[https://root.cern/doc/master/classTAttAxis.html#ae3067b6d4218970d09418291cbd84084](https://root.cern/doc/master/classTAttAxis.html#ae3067b6d4218970d09418291cbd84084)

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [December 20, 2023, 9:37am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/7 "2023-12-20T09:37:52Z")

</div>

Thanks a lot @couet !

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [December 20, 2023, 12:06pm UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/8 "2023-12-20T12:06:22Z")

</div>

Hi,

just to be clear: the `TAttAxis::SetNdivisions` has nothing to do with your problem. In [your previous post](https://root-forum.cern.ch/t/how-to-draw-a-bar-chart/57587) 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](https://root.cern.ch/root/html534/guides/users-guide/Histograms.html#constant-or-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`.

---

<div class="post-metadata">

**Author:** ![utkarsh-2000](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/e47774/32.png) [@utkarsh-2000](https://root-forum.cern.ch/u/utkarsh-2000)\
**Post date:** [December 20, 2023, 12:14pm UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/9 "2023-12-20T12:14:42Z")

</div>

> [@yus](#):
>
> `s::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](https://root.cern.ch/root/html534/guides/users-guide/Histograms.html#constant-or-variable-bin-width) instead, “splitting” your first bin (-0.5 \< ID \< +0.5) into two: -0.5 \< ID and ID \< +0.5. All other bins should be kept unchanged. Then you can “hide” your first bin (the one with -0.5 \< ID) using `TH1::SetRange` or `TH1::SetRangeUser`.

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

Thanks a lot for pointing out the solution.

---

<div class="post-metadata">

**Author:** ![SapphireShade](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sapphireshade/32/35327_2.png) [@SapphireShade](https://root-forum.cern.ch/u/SapphireShade)\
**Post date:** [December 21, 2023, 9:11am UTC](https://root-forum.cern.ch/t/how-to-set-x-axis-range-after-using-setndivisions/57597/10 "2023-12-21T09:11:36Z")

</div>

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