# Adjusting Y maximum range in pad automatically

**URL:** <https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801>\
**Category:** ROOT\
**Created:** [March 15, 2013, 3:24pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801 "2013-03-15T15:24:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmosquer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@cmosquer](https://root-forum.cern.ch/u/cmosquer)\
**Post date:** [March 15, 2013, 3:24pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/1 "2013-03-15T15:24:53Z")

</div>

Hi  
I am drawing a set of histograms in my canvas and I always find myself adjusting the Y axis range of my pad because I guess root is taking the Y axis values of the first plotted histogram TH1 to be the standard for the session.

I can write a script to loop through all my histograms so to estimate the ymax from all of them and then loop again to adjust the h-\>SetMaximum(ymax) of all the histograms. Is there a better and more elegant way to do it?

Like instead of calling the second loop, could I tell my gPad to readjust its axis?

I guess there is another way to go around this problem like using the THStack object… right?

Cheers,  
C.

---

<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:** [March 15, 2013, 3:28pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/2 "2013-03-15T15:28:22Z")

</div>

Use a [THStack](http://root.cern.ch/root/html/THStack.html) and draw it with the [“NOSTACK”](http://root.cern.ch/root/html/THistPainter.html#HP26) drawing option.

---

<div class="post-metadata">

**Author:** ![cmosquer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@cmosquer](https://root-forum.cern.ch/u/cmosquer)\
**Post date:** [March 25, 2013, 6:15pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/3 "2013-03-25T18:15:49Z")

</div>

Hi  
I implemented this option and it works… now I have another question. When I draw the canvas, I cannot longer interactively changed Yaxis dsiplay via TAxis::SetUserRange since the only way to modify the axis range is via THStack::SetMaximum() in the case of Y axis. I can do that on my code but is it possible to do it dynamically?

It would be very helpful, instead of compiling my code each time.

C Mos

---

<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:** [March 26, 2013, 9:18am UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/4 "2013-03-26T09:18:23Z")

</div>

I just tried with 5.34 to change interactively the range on the Y axis. It works.

---

<div class="post-metadata">

**Author:** ![cmosquer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@cmosquer](https://root-forum.cern.ch/u/cmosquer)\
**Post date:** [March 26, 2013, 1:06pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/5 "2013-03-26T13:06:17Z")

</div>

Yes, you can click and drag right on the axis to “zoom in” or you can unzoom via right-clicked menu. However if you want to set the scale precisely using SetUserRange(), it just doesn’t work for THStack objects drawn in my Canvas.  
Cheers,

---

<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:** [March 26, 2013, 1:12pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/6 "2013-03-26T13:12:03Z")

</div>

The Y axis’ range is always set via SetMaximum and SetMinimum. See:

> [@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…

---

<div class="post-metadata">

**Author:** ![cmosquer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@cmosquer](https://root-forum.cern.ch/u/cmosquer)\
**Post date:** [March 26, 2013, 1:20pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/7 "2013-03-26T13:20:07Z")

</div>

Thank you.  
I can set the ranges in my code, that is not a problem except that I have to re-compile and run each time. On the other hand, if I draw my histograms in my canvas directly, then i could adjust the axis with SetUserRange function when my histograms are already drawn.

I need to use the THStack in my specific case. I am just trying to figure out if there is an easy way to adjust the axis via SetUserRange-like function.

Maybe there is not way…

Cheers,

---

<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:** [March 26, 2013, 1:32pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/8 "2013-03-26T13:32:25Z")

</div>

```auto
{
   //-------------------------------------------

   TH1F *h1 = new TH1F("h1","Histo 1",100,-5,5);
   h1->FillRandom("gaus",10000);
   TH1F *h2 = new TH1F("h2","Histo 2",100,-5,5);
   h2->FillRandom("gaus",5000);
   TH1F *h3 = new TH1F("h3","Histo 3",100,-5,5);
   h3->FillRandom("gaus",2000);

   //-------------------------------------------

   h1->SetFillStyle(3004);
   h1->SetFillColor(1);

   h2->SetFillStyle(3004);
   h2->SetFillColor(2);

   h3->SetFillStyle(3004);
   h3->SetFillColor(3);

   //-------------------------------------------

   THStack *hs = new THStack("hs","A stack of histograms");

   hs->Add(h1);
   hs->Add(h2);
   hs->Add(h3);

   //-------------------------------------------
   TLegend *leg = new TLegend(0.1,0.6,0.4,0.9);

   leg->AddEntry(h1,"h1","f");
   leg->AddEntry(h2,"h2","f");
   leg->AddEntry(h3,"h3","f");
 
   //-------------------------------------------

   hs->Draw("nostack");
   hs->SetMinimum(100);
   hs->SetMaximum(200);
   leg->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![cmosquer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@cmosquer](https://root-forum.cern.ch/u/cmosquer)\
**Post date:** [March 26, 2013, 1:58pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/9 "2013-03-26T13:58:06Z")

</div>

Yes, my question is that after you execute the code and draw that, then if you right click on the y-axis and try to adjust the range using SetUserRange() function, it doesn’t work.

My program is not a macro so I have to recompile each time.

Cheers,

---

<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:** [March 26, 2013, 2:22pm UTC](https://root-forum.cern.ch/t/adjusting-y-maximum-range-in-pad-automatically/15801/10 "2013-03-26T14:22:08Z")

</div>

That’s because `SetRangeUser` has been done on all axis automatically when this menu is build there is no knowledge about the kind of axis (X Y or Z). The only way is to zoom using the mouse on the axis itself. But as you said it is less precise . In case of Y-Axis this option should be remove I guess.
