How to dynamically scale Y axis for multiple histograms on the same canvas


I’m trying to draw multiple histogram on the same canvas. I use h1->Draw() on the first one and h2->Draw(“same”) and so on, on the rest. The histograms are getting drawn fine. The problem however is that my newer (than the first) histograms are sometimes getting clipped at the top. It is because some of the newer ones have larger (than the older) Y values, and Draw(“same”) doesn’t seem to adjust the Y axis for the newer ones!

I saw a post that said I’ve to use THStack - but when I follow the example (hStack->Add(h1), hStack->Add(h2), …, hStack->Draw()), I get a empty canvas. Not sure what is going on there.

Another post mentioned h->Setbit(kCanRebin), but it seems that kCanRebin has been deprecated.

Root documentation mentions h->SetCanExtend(H1::kYaxis), but that is not making any difference for me.

What is the solution to this?

Thank you for your time and help,


Ok, I’ve figured it out. The example shown in the link below actually works.

The only change I had to make is,
THStack* hs = new THStack(“hs”,“test stacked histograms”);


I would recommend this version of the Ref Guide: