Home | News | Documentation | Download

Two legends on one canvas

Hey guys,
I have two histograms on one canvas and I want the fancy looking legend to be displayed for both H1 and H2. However, it is only displayed for H1. How can I get H2’s legend on the same canvas? I tried creating it manually (only to show entries for now) but it didn’t go as planned:

 legend1 = ROOT.TLegend(0.8, 0.5 , 0.95, 0.65)
                ROOT.SetOwnership(legend1,False)
                legend1.SetBorderSize(1)
                legend1.SetShadowColor(2)
                legend1.SetHeader("H2")
                legend1.AddEntry("entries","Entries   "+str(cutTree.GetEntries()))
                legend1.SetTextSize(0.04)
                legend1.SetTextColor(1)
                legend1.Draw("same")

h

(Ignore the text size and everything for now. It’s just a question of how do I get it to look as “formal” as it is for H1. Any help is appreciated

Diyon

Hello Diyon335,

Maybe @couet knows the answer to this question. Also, could you please send us the rest of the histogram-setup code?

Cheers,
J.

*tree and cutTree are just two decay trees but here is the set up for histograms

                c = ROOT.TCanvas("c")

                h1 = ROOT.TH1F("H1", h1T, hBins, hRange[0], hRange[1])
                tree.Draw(variable+">>H1("+str(hBins)+","+str(hRange[0])+","+str(hRange[1])+")")
                h1 = ROOT.gDirectory.Get("H1")
                h1.SetLineColor(4)
                h1.GetXaxis().SetTitle(x)
                h1.GetYaxis().SetTitle(y)
                h1.SetTitle(h1T)

                h2 = ROOT.TH1F("H2", h2T, hBins, hRange[0], hRange[1])
                cutTree.Draw(variable+">>H2("+str(hBins)+","+str(hRange[0])+","+str(hRange[1])+")")
                h2 = ROOT.gDirectory.Get("H2")
                h2.SetLineColor(3)
                h2.GetXaxis().SetTitle(x)
                h2.GetYaxis().SetTitle(y)
                h2.SetTitle(h2T)

                h1.Draw()
                h2.Draw("same")

                legend1 = ROOT.TLegend(0.8, 0.5 , 0.95, 0.65)
                ROOT.SetOwnership(legend1,False)
                legend1.SetBorderSize(1)
                legend1.SetShadowColor(2)
                legend1.SetHeader("H2")
                legend1.AddEntry("entries","Entries   "+str(cutTree.GetEntries()))
                legend1.SetTextSize(0.04)
                legend1.SetTextColor(1)
                legend1.Draw("same")
                
                legend = ROOT.TLegend(0.8, 0.3, 0.95, 0.45)
                ROOT.SetOwnership(legend, False)
                legend.SetBorderSize(1)
                legend.SetShadowColor(2)
                legend.AddEntry(h1, "Before", "l")
                legend.AddEntry(h2, "After", "l")
                legend.SetTextSize(0.03)
                legend.SetTextColor(1)
                legend.Draw("same")
                c.Update()

                c.Draw()
                c.Print(outFile,"PDF")

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