# Scaling histograms before stacking them with option "nostackb" removes the bins width and colors

**URL:** <https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573>\
**Category:** Newbie\
**Created:** [September 15, 2022, 7:49am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573 "2022-09-15T07:49:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxime\_Jotterand](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxime_jotterand/32/28169_2.png) [@Maxime\_Jotterand](https://root-forum.cern.ch/u/Maxime_Jotterand)\
**Post date:** [September 15, 2022, 7:49am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/1 "2022-09-15T07:49:06Z")

</div>

Hello,  
I am stacking 3 non-normalized histograms and assign each of them colors. The result is what I am looking for; visible bins with the respective color. Now, whenever I try to scale each histograms before integrating them to the THStack, the resulting plot loses the color and also plots crosses instead of bins. In other words, there are no more rectangular bins but only crosses with the same blue across every histograms. What do I don’t understand about this behaviour?

Here is some code to generate the results:  
`  
TH1F h1(“H”, “Bleu”, bin\_width, bin\_min, bin\_max), h2(“H”, “Green”, bin\_width, bin\_min, bin\_max),  
h3(“H”, “Red”, bin\_width, bin\_min, bin\_max);

// Fill the histograms  
…

// Scaling (this step is problematic when I introduce it, the plot is different and  
// there are not color differences (every crosses are the same blue across all histograms)  
h1.Scale(1/h1.Integral());  
h2.Scale(1/h2.Integral());  
h3.Scale(1/h3.Integral());

// Assign color  
h1.SetFillColor(kBlue);  
h2.SetFillColor(kGreen);  
h3.SetFillColor(kRed);

THStack hs(“hs”,“Bleu”);  
hs-\>Add(h1);  
hs-\>Add(h2)  
hs-\>Add(h3);

hs-\>Draw(“nostackb”);

Thank you in advance for your help.  
`

_ROOT Version:_ 6.26/06  
_Platform:_ Windows 10  
_Compiler:_ MSVC 17

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 15, 2022, 7:52am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/2 "2022-09-15T07:52:44Z")

</div>

@couet can you have a look? Thanks!

---

<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:** [September 15, 2022, 7:53am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/3 "2022-09-15T07:53:55Z")

</div>

A simple “fix” … after “`histo.Scale(...);`” execute “`histo.Sumw2(0);`”`

---

<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 15, 2022, 8:06am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/4 "2022-09-15T08:06:18Z")

</div>

… or add the histograms in the stack with the option “HIST”.

and BTW, Welcome to the ROOT forum 😉

---

<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:** [September 15, 2022, 8:11am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/5 "2022-09-15T08:11:20Z")

</div>

Or draw the stack with the option “hist”:

```auto
hs->Draw("nostackb, hist");

```

But using “hist” when adding them to hs, you can control each one:

```auto
hs->Add(h1,"hist");
hs->Add(h2); // if you don't want lines/bars for this one
hs->Add(h3,"hist");
hs->Draw("nostackb");

```

---

<div class="post-metadata">

**Author:** ![Maxime\_Jotterand](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxime_jotterand/32/28169_2.png) [@Maxime\_Jotterand](https://root-forum.cern.ch/u/Maxime_Jotterand)\
**Post date:** [September 15, 2022, 10:26am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/6 "2022-09-15T10:26:46Z")

</div>

Thank you, it also works like this. But I think the preferred solution is when adding the “hist” to the Add method of THStack.

---

<div class="post-metadata">

**Author:** ![Maxime\_Jotterand](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxime_jotterand/32/28169_2.png) [@Maxime\_Jotterand](https://root-forum.cern.ch/u/Maxime_Jotterand)\
**Post date:** [September 15, 2022, 10:28am UTC](https://root-forum.cern.ch/t/scaling-histograms-before-stacking-them-with-option-nostackb-removes-the-bins-width-and-colors/51573/7 "2022-09-15T10:28:05Z")

</div>

Thank you for the answer and for this handy library.
