
The following example shows how to create a legend. In this example the legend contains a histogram, a function and a graph. The histogram is put in the legend using its reference pointer whereas the graph and the function are added using their names. Note that, because TGraph contructors do not have the TGraph name as parameter, the graph name should be specified using the SetName method.
When an object is added by name, a scan is performed on the list of objects contained in the current pad (gPad) and also in the possible TMultiGraph and THStack present in the pad. If a matching name is found, the coresponding object is added in the legend using its pointer.

{
TCanvas *c1 = new TCanvas("c1","c1",600,500);
gStyle->SetOptStat(0);
TH1F *h1 = new TH1F("h1","TLegend Example",200,-10,10);
h1->FillRandom("gaus",30000);
h1->SetFillColor(kGreen);
h1->SetFillStyle(3003);
h1->Draw();
TF1 *f1=new TF1("f1","1000*TMath::Abs(sin(x)/x)",-10,10);
f1->SetLineColor(kBlue);
f1->SetLineWidth(4);
f1->Draw("same");
const Int_t n = 20;
Double_t x[n], y[n], ex[n], ey[n];
for (Int_t i=0;i<n;i++) {
x[i] = i*0.1;
y[i] = 1000*sin(x[i]+0.2);
x[i] = 17.8*x[i]-8.9;
ex[i] = 1.0;
ey[i] = 10.*i;
}
TGraphErrors *gr = new TGraphErrors(n,x,y,ex,ey);
gr->SetName("gr");
gr->SetLineColor(kRed);
gr->SetLineWidth(2);
gr->SetMarkerStyle(21);
gr->SetMarkerSize(1.3);
gr->SetMarkerColor(7);
gr->Draw("P");
leg = new TLegend(0.1,0.7,0.48,0.9);
leg->SetHeader("The Legend Title");
leg->AddEntry(h1,"Histogram filled with random numbers","f");
leg->AddEntry("f1","Function abs(#frac{sin(x)}{x})","l");
leg->AddEntry("gr","Graph with error bars","lep");
leg->Draw();
return c1;
}Note that the TPad class has a method to build automatically a legend for all objects in the pad. It is called TPad::BuildLegend().
Each item in the legend is added using the AddEntry method. This method defines the object to be added (by reference or name), the label associated to this object and an option which a combination of:
| virtual void | TObject::DoError(int level, const char* location, const char* fmt, va_list va) const |
| void | TObject::MakeZombie() |
| TLegend& | operator=(const TLegend&) |
| enum TPave::[unnamed] { | kNameIsAction | |
| }; | ||
| enum TBox::[unnamed] { | kCannotMove | |
| }; | ||
| enum TObject::EStatusBits { | kCanDelete | |
| kMustCleanup | ||
| kObjInCanvas | ||
| kIsReferenced | ||
| kHasUUID | ||
| kCannotPick | ||
| kNoContextMenu | ||
| kInvalidObject | ||
| }; | ||
| enum TObject::[unnamed] { | kIsOnHeap | |
| kNotDeleted | ||
| kZombie | ||
| kBitMask | ||
| kSingleKey | ||
| kOverwrite | ||
| kWriteDelete | ||
| }; |
| Int_t | TPave::fBorderSize | window box bordersize in pixels |
| Float_t | fColumnSeparation | separation between columns, as a fraction of |
| Double_t | TPave::fCornerRadius | Corner radius in case of option arc |
| Float_t | fEntrySeparation | separation between entries, as a fraction of |
| Color_t | TAttFill::fFillColor | fill area color |
| Style_t | TAttFill::fFillStyle | fill area style |
| Int_t | TPave::fInit | (=0 if transformation to NDC not yet done) |
| Color_t | TAttLine::fLineColor | line color |
| Style_t | TAttLine::fLineStyle | line style |
| Width_t | TAttLine::fLineWidth | line width |
| Float_t | fMargin | fraction of total width used for symbol |
| Int_t | fNColumns | number of columns in the legend |
| TString | TPave::fName | Pave name |
| TString | TPave::fOption | Pave style |
| TList* | fPrimitives | list of TLegendEntries |
| Bool_t | TBox::fResizing | !True if box is being resized |
| Int_t | TPave::fShadowColor | Color of the pave's shadow |
| Short_t | TAttText::fTextAlign | Text alignment |
| Float_t | TAttText::fTextAngle | Text angle |
| Color_t | TAttText::fTextColor | Text color index |
| Font_t | TAttText::fTextFont | Text font number |
| Float_t | TAttText::fTextSize | Text size |
| Double_t | TBox::fX1 | X of 1st point |
| Double_t | TPave::fX1NDC | X1 point in NDC coordinates |
| Double_t | TBox::fX2 | X of 2nd point |
| Double_t | TPave::fX2NDC | X2 point in NDC coordinates |
| Double_t | TBox::fY1 | Y of 1st point |
| Double_t | TPave::fY1NDC | Y1 point in NDC coordinates |
| Double_t | TBox::fY2 | Y of 2nd point |
| Double_t | TPave::fY2NDC | Y2 point in NDC coordinates |

Normal Contructor.
A TLegend is a Pave with several TLegendEntry(s). x1,y1,x2,y2 are the coordinates of the Legend in the current pad (in normalized coordinates by default) "header" is the title that will be displayed at the top of the legend it is treated like a regular entry and supports TLatex. The default is no header (header = 0). The options are the same as for TPave Default = "brNDC"
Add a new entry to this legend. "obj" is the object to be represented. "label" is the text you wish to associate with obj in the legend. If "label" is null or empty, the title of the object will be used.
Options are:
Add a new entry to this legend. "name" is the name of an object in the pad to be represented label is the text you wish to associate with obj in the legend if label is null or empty, the title of the object will be used.
Options are:
Get entry pointed to by the mouse. This method is mostly a tool for other methods inside this class.
Returns the header, which is the title that appears at the top of the legend.
Add a new entry before the entry at the mouse position.
Set the number of columns for the legend. The header, if set, is given its own row. After that, every nColumns entries are inserted into the same row. For example, if one calls legend.SetNColumns(2), and there is no header, then the first two TObjects added to the legend will be in the first row, the next two will appear in the second row, and so on.
Save this legend as C++ statements on output stream out to be used with the SaveAs .C option.
Sets the header, which is the "title" that appears at the top of the legend.
{ fEntrySeparation = 0.1f; fMargin = 0.25f; fNColumns = 1; fColumnSeparation = 0.0f; }