# Redraw TH1 and omit the empty histos

**URL:** <https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858>\
**Category:** ROOT\
**Created:** [March 29, 2013, 1:19am UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858 "2013-03-29T01:19:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alkass](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alkass](https://root-forum.cern.ch/u/Alkass)\
**Post date:** [March 29, 2013, 1:19am UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/1 "2013-03-29T01:19:30Z")

</div>

Hi

Probably, it has been already asked, but the proposed solutions (SetUserRange) could not work for me - What I want to do, is this - I do have collection of TH1 , and want to redraw all of them in a dynamic way so to omit the empty parts of the histos - For example, how is it possible to get rid of the empty left and right bins like in the attached histo ? I dont really care keeping or not the initial number of bins.

thanks

Alex

 ![](https://root-forum.cern.ch/uploads/default/original/2X/d/dba3e114eb5921e21526985ab0afbd7f6a9f9ef7.png)

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [March 29, 2013, 1:48am UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/2 "2013-03-29T01:48:17Z")

</div>

Why do SetRange and SetRangeUser not work? I recommend

```auto
h.GetXaxis()->SetRange(h.FindFirstBinAbove(0),h.FindLastBinAbove(0))
```

You may need to use -\> instead of . depending on how you do object/pointer stuff.

---

<div class="post-metadata">

**Author:** ![Alkass](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alkass](https://root-forum.cern.ch/u/Alkass)\
**Post date:** [March 29, 2013, 1:59am UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/3 "2013-03-29T01:59:12Z")

</div>

Thanks! This indeed works ! Btw, is there a similar way to do this for a TStack ?

Cheers

-a

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [March 29, 2013, 4:01am UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/4 "2013-03-29T04:01:59Z")

</div>

For a THStack you will need to loop over the contained histograms, or use the “last” histogram which contains the sum. See this discussion regarding the same issue in PyROOT:

> [@FindFirstBinAbove for THStack](https://root-forum.cern.ch/t/findfirstbinabove-for-thstack/15589/1):
>
> Hi, I quite enjoy making histograms with a fixed bin width where I set the maximum/minimum bins to very large values to avoid overflows. E.g. in PyROOT: binwidth = 0.1 minbin,maxbin = -1000,1000 nbins = (maxbin-minbin)/binwidth h = ROOT.TH1F("h","h",nbins,minbin,maxbin) Then to draw the histogram properly for looking at, I use: firstbin = h.FindFirstBinAbove(0) lastbin = h.FindLastBinAbove(0) h.GetXaxis().SetRange(firstbin,lastbin) I have now a THStack with multiple histograms in it, and it i…

Jean-François

---

<div class="post-metadata">

**Author:** ![Alkass](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alkass](https://root-forum.cern.ch/u/Alkass)\
**Post date:** [March 29, 2013, 12:22pm UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/5 "2013-03-29T12:22:58Z")

</div>

Hi Jean-Francois

Although this works in principle, the problem is that only the last histo of the stack (the sum) is drawn, meaning that you loose the color-code due to the “stacking” in the first place - In other words, how would be possible to change the range dynamically, but still get the full collection of the TStack in the final plot ?

thanks

Alex

---

<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 29, 2013, 12:37pm UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/6 "2013-03-29T12:37:41Z")

</div>

You should use the “SUM” only for the purpose of finding the first and the last nonzero bins. Draw the “full” THStack (e.g. with “NOSTACK”) and then “limit” the x-axis according to the found nonzero bins.

---

<div class="post-metadata">

**Author:** ![Alkass](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alkass](https://root-forum.cern.ch/u/Alkass)\
**Post date:** [March 29, 2013, 12:42pm UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/7 "2013-03-29T12:42:09Z")

</div>

Hi

Sorry for my ignorance, is that exact part I do not know how to do, “limit the xaxis” - How can this be possible ? Can you provide a working example

Doing something like

```auto

```

gives

`Error: illegal pointer to class object GetXaxis() 0x0 
`

-a

---

<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 29, 2013, 12:49pm UTC](https://root-forum.cern.ch/t/redraw-th1-and-omit-the-empty-histos/15858/8 "2013-03-29T12:49:55Z")

</div>

See: [url][SetAxisRange(0.0,1.0,"Y"); is ineffective](https://root-forum.cern.ch/t/setaxisrange-0-0-1-0-y-is-ineffective/14040/1)

Note: you MUST draw the THStack BEFORE you can retrieve its x-axis. You might also make sure that it is really drawn by calling “gPad-\>Modified(); gPad-\>Update();”.
