Drawing histogram ratio as bars

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):

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:


Is there any simple way of doing this by using the lines above?

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

Have you tried: ROOT: TRatioPlot Class Reference

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

1 Like

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 :slight_smile:

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

Hi @couet, here is a reproducer of my current method (with customisations) in case you want to have a look:
example_plot.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.

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:

ratiosSM.Draw("hist") 

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

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

And you need to call:

gStyle->SetHistMinimumZero();
1 Like

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()?

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

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

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.

1 Like

Maybe instead of a “Data/SM” ratio, you want a “log(Data/SM)” or a "diff" or a "diffsig".

This worked great, thanks!