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)

pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0)
pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.3)

canvas.cd()
pad1.SetBottomMargin(0)
pad1.Draw()
pad1.cd()

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

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

pad1.SetGridx()
pad1.SetGridy()

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

canvas.cd()
pad2.SetTopMargin(0)
pad2.SetBottomMargin(0.2)
pad2.SetGridx()
pad2.SetGridy()
pad2.Draw()
pad2.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.Add(hBkg)
hMC.Add(hSig)
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):

pad2.SetBottomMargin(0.3)
#...
ratios.GetXaxis().SetTitleSize(15)
ratios.GetXaxis().SetTitleFont(43)
ratios.GetXaxis().SetLabelFont(43)
ratios.GetXaxis().SetLabelSize(15)

This worked great, thanks a lot!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.