# Drawing histogram ratio as bars

**URL:** <https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801>\
**Category:** Newbie\
**Tags:** python, hist, root\
**Created:** [February 27, 2023, 11:31am UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801 "2023-02-27T11:31:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yburkard](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@yburkard](https://root-forum.cern.ch/u/yburkard)\
**Post date:** [February 27, 2023, 11:31am UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/1 "2023-02-27T11:31:26Z")

</div>

Dear Users,

I have a simple question which I could not find the answer to. I want to draw a histogram ratio plot as bars starting from the line y=1. My code lines look as follows (`hist1` and `hist2` are `TH1D` objects):

```auto
ratios = hist1.Clone("ratios")
ratios.Divide(hist2)
ratios.Draw("ep")

```

Setting the option `"ep"` draws points with error bars. I have tried calling `"hist"`, but the problem is that the bars start from the line y=0. Essentially I would like to to obtain something line this:

 ![Screenshot 2023-02-27 at 12.29.44](https://root-forum.cern.ch/uploads/default/original/3X/a/6/a66b3c8b4e2a2b7aabbc4da90f35a2cb2b5a1787.png)  
Is there any simple way of doing this by using the lines above?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [February 27, 2023, 12:47pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/2 "2023-02-27T12:47:55Z")

</div>

I’m sure @couet will be able to help you

---

<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:** [February 27, 2023, 2:00pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/3 "2023-02-27T14:00:12Z")

</div>

Have you tried: [ROOT: TRatioPlot Class Reference](https://root.cern/doc/master/classTRatioPlot.html#a85fe584276d343721cb952b64bc82297)

?  
In principle what you are looking for is the default. See the first example here:  
[https://root.cern/doc/master/classTRatioPlot.html#a85fe584276d343721cb952b64bc82297](https://root.cern/doc/master/classTRatioPlot.html#a85fe584276d343721cb952b64bc82297)

---

<div class="post-metadata">

**Author:** ![yburkard](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@yburkard](https://root-forum.cern.ch/u/yburkard)\
**Post date:** [February 27, 2023, 3:17pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/4 "2023-02-27T15:17:07Z")

</div>

Thanks for the suggestion @couet, your link sends me to the function `SetH1DrawOpt()`; do you mean that by calling it I will automatically obtain bars as histograms? Wouldn’t there perhaps be a simple way of ‘shifting’ vertically my ratio plot so that the bottom is at 1?  
I have already customised a second ratio plot (as dots with error bars) using the `Divide` function, and I believe it would take me a while until I translate all details to TRatioPlot, but if there is no direct way of doing this with my method I will then give TRatioPlot a try 🙂

---

<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:** [February 27, 2023, 4:24pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/5 "2023-02-27T16:24:19Z")

</div>

> [@yburkard](#):
>
> Essentially I would like to to obtain something line this:

Can you provide a reproducer? what you are trying to get seems not possible.  
The histogram bar chart drawing start at 0. Like in this example:  
[https://root.cern/doc/master/classTHistPainter.html#HP100](https://root.cern/doc/master/classTHistPainter.html#HP100)

---

<div class="post-metadata">

**Author:** ![yburkard](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@yburkard](https://root-forum.cern.ch/u/yburkard)\
**Post date:** [February 27, 2023, 10:46pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/6 "2023-02-27T22:46:02Z")

</div>

Hi @couet, here is a reproducer of my current method (with customisations) in case you want to have a look:  
[example\_plot.py](https://root-forum.cern.ch/uploads/short-url/mzlC0PcOn9SLmIlLe4E3YZsOVWG.py) (2.8 KB)

One possible idea that came to mind is to subtract a dummy histogram [1,… , 1] from the ratio plot so that I would obtain the desired bars, although they would start from 0 and not from 1, ideally I would then “shift” the y-axis by -1, but I would not know how to do this in ROOT.

---

<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:** [February 28, 2023, 6:35am UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/7 "2023-02-28T06:35:35Z")

</div>

Thanks for your code. I thought you were using TRatioPlot, but indeed you are doing your own “ratio plot”. That’s fine. In your script, the bottom plot is produce with:

```auto
ratiosSM.Draw("hist") 

```

Yes you can shift by -1 the top plot. For instance with:

```auto
for (int i = 1; i <= hist->GetNbinsX(); i++) ratiosSM>SetBinContent(i, ratiosSM->GetBinContent(i)-1);

```

And you need to call:

```auto
gStyle->SetHistMinimumZero();

```

---

<div class="post-metadata">

**Author:** ![yburkard](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@yburkard](https://root-forum.cern.ch/u/yburkard)\
**Post date:** [February 28, 2023, 1:38pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/8 "2023-02-28T13:38:03Z")

</div>

Thanks a lot @couet, that worked perfectly! One last question I would have is if there is a way of “shifting” the y-axis values by -1? Perhaps with `ratiosSM.GetYaxis()`?

---

<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:** [February 28, 2023, 1:51pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/9 "2023-02-28T13:51:59Z")

</div>

Ah … you mean having the behavior you get but the labels being all \> 0 ?

---

<div class="post-metadata">

**Author:** ![yburkard](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@yburkard](https://root-forum.cern.ch/u/yburkard)\
**Post date:** [February 28, 2023, 2:08pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/10 "2023-02-28T14:08:48Z")

</div>

exactly, so that I still have ‘Data/SM’ on the y-axis and not ‘Data/SM - 1’

---

<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:** [February 28, 2023, 2:46pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/11 "2023-02-28T14:46:52Z")

</div>

The axis labels are automatically computed according to the histogram contain.  
The only trick you can do is to use ChangeLabel to define the label you want… but that’s  
will not be automatic anymore.

---

<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:** [February 28, 2023, 2:51pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/12 "2023-02-28T14:51:24Z")

</div>

Maybe instead of a “Data/SM” ratio, you want a “log(Data/SM)” or [a `"diff"` or a `"diffsig"`](https://root.cern/doc/master/classTRatioPlot.html).

---

<div class="post-metadata">

**Author:** ![yburkard](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/e480ec/32.png) [@yburkard](https://root-forum.cern.ch/u/yburkard)\
**Post date:** [February 28, 2023, 4:12pm UTC](https://root-forum.cern.ch/t/drawing-histogram-ratio-as-bars/53801/13 "2023-02-28T16:12:57Z")

</div>

> [@couet](#):
>
> ChangeLabel

This worked great, thanks!
