TH2F *h2d = new TH2F("h2d","2D Histogram",100,0,1,100,0,1); // Set title for x and y axis h2d->GetXaxis()->SetTitle("x-axis"); h2d->GetYaxis()->SetTitle("y-axis"); // Create a legend TLegend *legend = new TLegend(0.5,0.5,0.9,0.9); legend->SetHeader("Legend Title"); // Add an entry to the legend legend->AddEntry(h2d,"My Histogram Title","l"); // Draw the histogram and legend h2d->Draw("colz"); legend->Draw("same");
TCanvas *c = new TCanvas("c","c",800,600); // Create four TGraph objects TGraph *gr1 = new TGraph(); TGraph *gr2 = new TGraph(); TGraph *gr3 = new TGraph(); TGraph *gr4 = new TGraph(); // Add some points to the TGraph objects // Create a legend TLegend *legend = new TLegend(0.1,0.1,0.4,0.4); legend->SetHeader("Legend Title"); // Add multiple entries to the legend legend->AddEntry(gr1,"Graph 1","lp"); legend->AddEntry(gr2,"Graph 2","lp"); legend->AddEntry(gr3,"Graph 3","lp"); legend->AddEntry(gr4,"Graph 4","lp"); // Draw the TGraph objects and legend gr1->Draw("AL"); gr2->Draw("L same"); gr3->Draw("L same"); gr4->Draw("L same"); legend->Draw();In this example, four TGraph objects are created and some points are added to them. Then, a legend is created with a custom location and title. Multiple entries are added to the legend for each TGraph object with labels "Graph 1", "Graph 2", "Graph 3", and "Graph 4". Finally, the TGraph objects and legend are drawn on the canvas. The TLegend class is part of the ROOT C++ package/library.