# Re-applying a zoom made by the user

**URL:** <https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988>\
**Category:** ROOT\
**Created:** [April 20, 2010, 9:14am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988 "2010-04-20T09:14:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barth](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@Barth](https://root-forum.cern.ch/u/Barth)\
**Post date:** [April 20, 2010, 9:14am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/1 "2010-04-20T09:14:36Z")

</div>

Dear all,

I would like to re-apply the zoom made by the user on an histogram.  
This means

1. find out the user range of the histo and
2. set this range on the same or another histogram.

I tried the following :

[code]TCanvas c;  
TH1F h(“adsf”, “asdf”, 10, 1, 10)  
h-\>Fill(5)  
h-\>Draw("")  
TAxis \*axis = h.GetXaxis()

// USER : Zoom with the mouse

// find out the displayed range  
xmin = gPad-\>GetUxmin()  
xmax = gPad-\>GetUxmax()

// USER : Do something

// Re-apply the zoom  
axis-\>SetRangeUser(xmin, xmax)  
gPad-\>Modified()  
// --\> Not what I expected as the axis moved  
[/code]

Unfortunately, this doesn’t work as I expected because the axis “moves”.

Then, I realized that adding or removing a little bit to xmin and xmax, does the trick :

` xmin = xmin + xmin / 15;
xmax = xmax - xmax / 15;`

However, I am wondering if there is a nice way of doing it instead of applying this magical factor 15 ?

Thanks in advance,  
Barth

---

<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:** [April 20, 2010, 9:26am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/2 "2010-04-20T09:26:13Z")

</div>

That is the margins size. You should not use 15. Get the size of the margins instead.

---

<div class="post-metadata">

**Author:** ![Barth](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@Barth](https://root-forum.cern.ch/u/Barth)\
**Post date:** [April 20, 2010, 9:27am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/3 "2010-04-20T09:27:04Z")

</div>

Hi again,

It appears that the factor 15 I was applying is not working neither.

So I am interested in any way to solve this question 🙂

Thanks  
Barth

---

<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:** [April 20, 2010, 9:28am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/4 "2010-04-20T09:28:30Z")

</div>

Have you seen my reply ? it looks like we wrote the last posts at the same time. Use the margins size.

---

<div class="post-metadata">

**Author:** ![Barth](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@Barth](https://root-forum.cern.ch/u/Barth)\
**Post date:** [April 20, 2010, 9:46am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/5 "2010-04-20T09:46:48Z")

</div>

Hi,

Indeed, I haven’t seen your post 🙂

Thanks, I will try that.

Barth

---

<div class="post-metadata">

**Author:** ![Barth](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@Barth](https://root-forum.cern.ch/u/Barth)\
**Post date:** [April 20, 2010, 10:18am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/6 "2010-04-20T10:18:43Z")

</div>

Hi again,

Could you tell me which margin size you are refering to ?  
I see several margins in TStyle, but they seem too big…

Thanks  
Barth

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [April 20, 2010, 10:38am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/7 "2010-04-20T10:38:29Z")

</div>

Could you reformulate your question?

if you zoom with the mouse a histogram TH1\* h, then if you do again h-\>Draw() the histogram  
witll be drawn with the range selected with the mouse, you do not have to set its range again.  
If you want to set the same zoom area for another histogram h2 having the same original limits than h, you can do something like

` TAxis *axis = h->GetXaxis();
   int first = axis->GetFirst();
   int last = axis->GetLast();
   h2->GetXaxis()->SetRange(first,last);
`  
if your second histogram h2 has different binning, do something like:

` TAxis *axis = h->GetXaxis();
   int first = axis->GetFirst();
   int last = axis->GetLast();
   h2->GetXaxis()->SetRangeUser(axis->GetBinCenter(first),axis->GetBinCenter(last));
`  
Rene

---

<div class="post-metadata">

**Author:** ![Barth](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f475e1/32.png) [@Barth](https://root-forum.cern.ch/u/Barth)\
**Post date:** [April 21, 2010, 7:29am UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/8 "2010-04-21T07:29:48Z")

</div>

Hello,

First of all, thank you, your solution solves my problem perfectly.

Now, the question was exactly what you thought : how to apply to an histogram h2 the zoom that a user made on histogram h1.

I was stuck with gPad-\>GetUxmin() and didn’t think about the axis’ first and last bin.

thanks,  
Barth

---

<div class="post-metadata">

**Author:** ![Viesturs](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/7cd45c/32.png) [@Viesturs](https://root-forum.cern.ch/u/Viesturs)\
**Post date:** [November 15, 2012, 1:22pm UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/9 "2012-11-15T13:22:14Z")

</div>

Hello!  
In my case I want to zoom a TProfile. The error with using GetYAxis is the following.  
“error: ‘class TProfile’ has no member named ‘GetYAxis’”

Nevertheless, it is till important for me to zoom on the Y axis.  
How can I do it?

Best regards,  
Viesturs

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 15, 2012, 1:24pm UTC](https://root-forum.cern.ch/t/re-applying-a-zoom-made-by-the-user/9988/10 "2012-11-15T13:24:42Z")

</div>

GetYaxis

See also: [url][TGraph title and axis problems](https://root-forum.cern.ch/t/tgraph-title-and-axis-problems/15241/2)
