# Changing the Range of x-axis

**URL:** <https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096>\
**Category:** ROOT\
**Created:** [September 18, 2011, 9:22pm UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096 "2011-09-18T21:22:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonyctam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jasonyctam](https://root-forum.cern.ch/u/jasonyctam)\
**Post date:** [September 18, 2011, 9:22pm UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/1 "2011-09-18T21:22:41Z")

</div>

Hi All,

I have written some analysis code to output my results as histograms, so that I can then scale, stack, and rebin however I want without having to run through the data files again.

I now need to change the x-axis range from the initial (0,1000) to (0,500), I have tried using

```auto

```

but it does nothing more than changing the axis to (0.,500.) the bins in the histogram does not change with it (ie a peak at 200 will now be at 100).

Does any one know how to do this without using something like SetRangeUser, which requires the use of the index of the bins?

Cheers  
Jason

---

<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:** [September 19, 2011, 3:12am UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/2 "2011-09-19T03:12:24Z")

</div>

Hi Jason,

SetRangeUser does not require usage of bins indices, it’s SetRange that requires it. So you may wanna try

```auto

```

it should work.

---

<div class="post-metadata">

**Author:** ![jasonyctam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jasonyctam](https://root-forum.cern.ch/u/jasonyctam)\
**Post date:** [September 19, 2011, 7:51am UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/3 "2011-09-19T07:51:33Z")

</div>

Hi Yus,

I have tried that already, however it also only does exactly what I described above, the bins do not change accordingly with the range of the axis, ie the peak at 200 now shows at 100, after going from (0,1000) to (0,500).

Jason

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [September 19, 2011, 8:02am UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/4 "2011-09-19T08:02:45Z")

</div>

Honestly speaking, I don’t really understand what you’re trying to achieve.  
Maybe this old thread will be of any help:  
[url][Can we shift histogram for several channels?](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/1)

---

<div class="post-metadata">

**Author:** ![jasonyctam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@jasonyctam](https://root-forum.cern.ch/u/jasonyctam)\
**Post date:** [September 19, 2011, 8:25am UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/5 "2011-09-19T08:25:15Z")

</div>

Hi Pepe Le pew,

I have looked at that thread and doesn’t seem to have the answer that I am looking for.

I have produced a histogram that initially has the range (0,1000) on the x-axis, and I observe a peak at 200, 1/5 of the whole x-axis counting from the left.

I now only want to keep the left half of the histogram, the (0,500) range. If I use SetLimits() like I said above, the only change that I see is the numbers on the axis, in this case divided by 2.

The entire histogram however do not change with it, the peak that was initially lined up at the 200 mark on the initial x-axis, now lines up with the 100 mark. Remaining at the 1/5 mark of the entire x-axis range counting from the left, instead of moving accordingly to the new location of the 200 mark, which would now be 2/5 of the whole axis counting from the left.

Again I have looked at SetRangeUser(), it doesn’t change anything.

I hope this longer explanation of my problem has made things more clear, rather than complicating it even more.

Cheers  
Jason

---

<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:** [September 19, 2011, 8:56am UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/6 "2011-09-19T08:56:54Z")

</div>

One way to help you further would be to have a small macro reproducing the problem.

You can try one more thing:  
In the graphics window set the range you need using the mouse along the X axis.  
If you manage to get what you want, then do “Save As …” in a .C file from the the “File” canvas menu.  
Then look at that .C file to see what it does.

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [September 19, 2011, 9:26am UTC](https://root-forum.cern.ch/t/changing-the-range-of-x-axis/13096/7 "2011-09-19T09:26:28Z")

</div>

Now it is more clear.  
Still, when you say “I now only want to keep the left half of the histogram”, do you mean that you want to “completely remove” the remaining bins (i.e. the “right half”) from your histogram, or do you mean that they can stay in your histogram and you simply want to display the “left half” only?  
In the latter case, for a TH1 I am usually using something like this:  
histo-\>Draw();  
histo-\>SetAxisRange(xmin, xmax, “X”);  
histo-\>SetAxisRange(ymin, ymax, “Y”);  
histo-\>Draw(); // make sure it’s redrawn  
If I am dealing with a THStack of several TH1 histograms, I am usually using something like this:  
stack-\>Draw(“H”);  
stack-\>GetXaxis()-\>SetLimits(xmin, xmax);  
stack-\>SetMinimum(ymin);  
stack-\>SetMaximum(ymax);  
stack-\>Draw(“H”); // make sure it’s redrawn  
If you want to “delete” unwanted bins from your histogram (i.e. the “right half”) then I don’t know of any easy way (except creating a new histogram with “cut axes” and copying the “wanted” contents only).
