# Problem setting y- and x-axsis titles for histogram, and extending range of y-axis

Dear Users,

I am plotting histograms with THStack and then taking their ratio, but I am having difficulties in generating titles for their axes, as well as changing the range of the y-axis. Here is the relevant part of my code:

canvas = TCanvas("Histogram", "Histogram", 100, 200, 700, 500)
legend = TLegend(0.70,0.65,0.85,0.85)

canvas.cd()

histStacked = THStack("histStacked","")
histStacked.Draw("hist")
histData.Draw("same E1P")

#trying this messes up the plot
#histStacked.GetYaxis().SetTitle("Frequency")
histStacked.GetYaxis().SetRange(0,150) #not working

ratios = histData.Clone("ratios")
ratios.Divide(histStacked.GetStack().Last())

canvas.cd()

ratios.Draw("ep")
ratios.SetTitle("")
ratios.SetStats(0)
ratios.GetYaxis().SetTitle("Data/(SM+BSM)") #working
ratios.GetXaxis().SetTitle("m_{ll}") #not working

Trying out histStacked.GetYaxis().SetTitle("Frequency") for the y-title takes away the filling from the histograms, and calling ratios.GetXaxis().SetTitle("m_{ll}") for the x-title and histStacked.GetYaxis().SetRange(0,150) or even histStacked.GetYaxis().SetRangeUser(0,150) for the change in y-range has no effect on the plot. Is there perhaps another way of doing this? Any help would be much appreciated!

I suppose these are 1-D histograms. These are not binned in the y-axis, so you cannot do histStacked.GetYaxis().SetRange(0,150). Try instead

histStacked.SetMinimum(yourMinY)
histStacked.SetMaximum(yourMaxY)

what do you mean by takes away the filling?

Hi @dastudillo, that worked great, thanks a lot! With “takes away the filling” I mean that after having filled the histograms with colour (e.g. calling hist1.SetFillColor(kGreen)), if I call histStacked.GetYaxis().SetTitle("Frequency") these fillings are removed.

I think you need to show more code, where are you doing that and so on; I could guess you need to include the option “f” when adding it to the THStack, but there could be other things going on. A complete but small reproducer would be best.

@dastudillo I have written a small, self-contained reproducer:
example_histPlot.py (1.9 KB)
The problematic commands are in lines 40 and 69 (I commented a short description of the respective issues). I hope this is clear enough, but let me know if you have further questions.

About 40: try doing the ‘customize’ lines (SetLine/FillColor) before adding the histograms to THStack:

#customize
hData.SetLineColor(kBlue)
hBkg.SetLineColor(kGreen)
hBkg.SetFillColor(kGreen)
hSig.SetLineColor(kRed)
hSig.SetFillColor(kRed)

hMC = THStack("histMC","")
hMC.Draw("hist")
hData.Draw("same E1P")

hMC.GetYaxis().SetTitle("Frequency")

About 69: the x-axis title does show, but it’s very small (zoom in on the PNG and you’ll see), like the axis labels. You are only changing the title and label font properties for the y-axis, but not for x, so add that (and you’ll need to increase the bottom margin of that pad):