Vertical axis range in THStack

Hi experts,
I’m having some trouble getting the desired result trying to set the range of the vertical axis when drawing a THStack. Take this code for example (running this in ROOT 5.34):

TH1D *hist = new TH1D(“hist”, “”, 2, 0, 1);
TH1D *hist2 = new TH1D(“hist2”, “”, 2, 0, 1);
hist->SetBinContent(1, .25);
hist->SetBinContent(2, .75);
hist2->SetBinContent(1, .75);
hist2->SetBinContent(2, .25);

THStack *stack = new THStack(“stack”, “stack”);


I would like the range of the vertical axis to be exactly 0 - 1, but I get a maximum slightly larger than 1 (and if I say SetMaximum(0.8 ) I get something around 0.9). I also tried stack->GetYaxis()->SetRangeUser(0, 1), but that didn’t work either. What does work is to interactively zoom the vertical axis, then right click it and select SetRangeUser, enter 0 and 1, and I get what I want. Any idea what I need to do to achieve this in my script without manual intervention?

Thanks in advance for your help!


[quote=“couet”] gStyle->SetHistTopMargin(0.); [/quote]

That worked. Thanks so much! I would never have found this by myself.


Yes, to get nice plots there is an automatic gap between the top of the histogram and the top of the frame. In 90% of the cases we want a such margin… except in some cases like yours. That’s why this value can be adjusted.