I am trying to figure out how to plot something on a linear axis and then plot another quantity on a log axis on the same plot and then have the two y-axes be on different scales as well. I’m currently attempting to do this with drawing the first histogram on a TPad
and then drawing the second histogram on a transparent TPad
above it. However, I am not sure how to get a log scale for the second histogram (the log scale is a property of the TCanvas
I think, not a TPad
) and then how to use TGaxis
properly with the log scale and second histogram.
Any suggestions are very welcome.
I’m attached an example RootPy
script that mocks up a demo of something close to what I’m trying to do (linear_log_axes_test.py (1.7 KB)) and I’m running it on lxplus with
$ lsetup root
$ root-config --version
6.10/04
The script contents and output are below as well
import ROOT
ROOT.gROOT.SetBatch(True) # Supress output to screen
def main():
# Create the canvas
can = ROOT.TCanvas('c', '', 800, 600)
x_range = [10, 70]
# Use a base pad and then a transparent pad
base_pad = ROOT.TPad('base_pad', '', 0, 0, 1, 1)
clear_pad = ROOT.TPad('clear_pad', '', 0, 0, 1, 1)
clear_pad.SetFillColor(0)
clear_pad.SetFillStyle(4000)
clear_pad.SetFrameFillStyle(0)
base_pad.Draw()
base_pad.cd()
# Turn off the tick marks on the right hand side y-axis
base_pad.SetTicky(0)
h1 = ROOT.TH1F('h1', '', 5, x_range[0], x_range[1])
for itr in range(0, 1001):
h1.Fill(ROOT.gRandom.Gaus(40, 5))
h1.Scale(1 / h1.Integral())
h1.SetLineColor(ROOT.kBlack)
h1.GetYaxis().SetRangeUser(0, 1.4)
h1.GetYaxis().SetTitle('Hist 1 axis label')
h1.GetXaxis().SetTitle('x axis label')
h1.Draw()
# distiguish visually efficiency of 1
eff_of_one_line = ROOT.TLine(x_range[0], 1.0, x_range[1], 1.0)
eff_of_one_line.SetLineStyle(2)
eff_of_one_line.Draw('SAME')
clear_pad.Draw()
clear_pad.cd()
clear_pad.SetTicks(0, 0)
# The pad should be on a log scale, but how?
can.SetLogy(1)
h2 = ROOT.TH1F('h2', '', 5, x_range[0], x_range[1])
for bin in range(1, 6):
h2.SetBinContent(bin, 0.2 * h1.GetBinContent(bin))
h2.Scale(1 / h2.Integral())
h2.SetLineColor(ROOT.kBlue)
h2.Draw('A')
log_axis_y_min = 0
log_axis_y_max = 1
log_axis = ROOT.TGaxis(70, 0, 70, 1, log_axis_y_min,
log_axis_y_max, 510, '+L')
log_axis.SetTitle('hist 2 axis label')
log_axis.Draw()
can.SaveAs('linear_log_axes_test.pdf')
if __name__ == '__main__':
main()