How to Add Legend?

I am attaching my program where I have made a multi graph on same pad and also added a new pad with another graph and second Y- Axis. Now I want to add legend to these graphs. I wrote the following code but the legends are not being printed.

{
   auto c1 = new TCanvas("c1","photon flux for varying Tungsten thickness",1500,500);	//(name, title, xlength, y length)
   auto pad = new TPad("pad","",0,0,1,1);						//(name, title, xlow, ylow, xup, yup)
   pad->SetGrid();
   pad->Draw();
   pad->SetLogy(1);
   pad->cd();

   // create multiple graphs
   
   TGraph *g1 = new TGraph("phflx_0.1.dat");
   g1->SetLineColor(1);
   g1->SetLineWidth(2);
   TGraph *g2 = new TGraph("phflx_0.2.dat");
   g2->SetLineColor(5);
   g2->SetLineWidth(2);
   TGraph *g5 = new TGraph("phflx_0.5.dat");
   g5->SetLineColor(8);
   g5->SetLineWidth(2);
   TGraph *g8 = new TGraph("phflx_0.8.dat");
   g8->SetLineColor(2);
   g8->SetLineWidth(2);
   TGraph *g15 = new TGraph("phflx_1.5.dat");
   g15->SetLineColor(4);
   g15->SetLineWidth(2);
   

   // create a multigraph and draw it
   TMultiGraph  *mg  = new TMultiGraph();
   mg->Add(g1);
   mg->Add(g2);
   mg->Add(g5);
   mg->Add(g8);
   mg->Add(g15);
   mg->SetTitle("Photon Flux for various Target Thickness; Photon Energy (MeV); Photon Flux per unit area per electron");
   mg->Draw("ALP");


   //create a transparent pad drawn on top of the main pad
   c1->cd();
   auto overlay = new TPad("overlay","",0,0,1,1);
   overlay->SetFillStyle(4000);
   overlay->SetFillColor(0);
   overlay->SetFrameFillStyle(4000);
   overlay->Draw();
   overlay->cd();
   //Create the second graph
   auto gdr= new TGraph("cross_section.dat");
   gdr->SetMarkerColor(kRed);
   gdr->SetMarkerStyle(5);
   gdr->SetName("gdr");
   TH1F *hframe = overlay->DrawFrame(-1.4,0,31.4,200);
   hframe->GetXaxis()->SetLabelOffset(99);
   hframe->GetYaxis()->SetLabelOffset(99);
   hframe->GetYaxis()->SetTickSize(0);
   gdr->Draw("PL");

   //Draw an axis on the right side
   TGaxis *axis = new TGaxis(31.4,0,31.4,200,0,200,80506,"+L");		
   axis->SetLineColor(kRed);
   axis->SetLabelColor(kRed);
   axis->SetLabelOffset(0.02);
   axis->Draw();


   //Add Legend 
   TLegend *legend = new TLegend(0.1,0.7,0.48,0.9);
   legend->SetHeader("Header","C"); 				// option "C" allows to center the header
   legend->AddEntry("g1","0.1 radiation length","l");
   legend->AddEntry("g2","0.2 radiation length","l");
   legend->AddEntry("g5","0.5 radiation length","l");
   legend->AddEntry("g8","0.8 radiation length","l");
   legend->AddEntry("g15","1.5 radiation length","l");
   legend->Draw();


}

Kindly Help.


_ROOT Version: 5.34/36
_Platform: CentOS &
_Compiler: Red Hat 4.8.5-28


I do not see why it should not work.
May be try to add the graphs by address:

 legend->AddEntry(g1,"0.1 radiation length","l");
...

Yes I tried that also. But that doesn’t work either. Can it be because of having two pads in my canvas?

I cannot run your macro because of the missing data files. Can you provide a running example showing the problem ?

I am getting the following error regarding TLegend

Error: Can't call TLegend::SetHeader("Header","C") in current scope GDR_various_thickness.c:112:
Possible candidates are...
(in TLegend)
/home/aqsa/Downloads/ROOT/root/lib/libGraf.so  -1:-1   0 public: virtual void TLegend::SetHeader(const char* header=""); //*MENU*
(in TPave)
(in TBox)
*** Interpreter error recovered ***

In ROOT version 5.34 TLegend::SetHeader has only only one parameter:
https://root.cern.ch/root/html534/TLegend.html#TLegend:SetHeader

It works now. Thank you. How can I set the legend colour differently for different graphs? All my graph lines are of different colour So I want the legend enteries to be of the same colour as the line colour. Right now all my legend entries are of black colour.

   legend->AddEntry("g1","0.1 radiation length","l");

Your graphs have no names so you need to use pointers: legend->AddEntry(g1, "0.1 radiation length", "l");

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