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; }
leg->SetTextAlign(13);leg->SetTextAlign(11); TLegendEntry *le = leg->AddEntry(h1,"Histogram filled with random numbers","f"); le->SetTextColor(kBlue);;
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:
As shown in the following example, passing a NULL pointer as first parameter in AddEntry is also valid. This allows to add text or blank lines in a legend.
{ TCanvas *c2 = new TCanvas("c2","c2",500,300); TLegend* leg = new TLegend(0.2, 0.2, .8, .8); TH1* h = new TH1F("", "", 1, 0, 1); leg->AddEntry(h, "Histogram \"h\"", "l"); leg->AddEntry((TObject*)0, "", ""); leg->AddEntry((TObject*)0, "Some text", ""); leg->AddEntry((TObject*)0, "", ""); leg->AddEntry(h, "Histogram \"h\" again", "l"); leg->Draw(); return c2; }
{ TCanvas *c3 = new TCanvas("c2","c2",500,300); TLegend* leg = new TLegend(0.2, 0.2, .8, .8); TH1* h = new TH1F("", "", 1, 0, 1); leg-> SetNColumns(2); leg->AddEntry(h, "Column 1 line 1", "l"); leg->AddEntry(h, "Column 2 line 1", "l"); leg->AddEntry(h, "Column 1 line 2", "l"); leg->AddEntry(h, "Column 2 line 2", "l"); leg->Draw(); return c3; }
virtual | ~TLegend() |
void | TObject::AbstractMethod(const char* method) const |
TLegendEntry* | AddEntry(const TObject* obj, const char* label = "", Option_t* option = "lpf") |
TLegendEntry* | AddEntry(const char* name, const char* label = "", Option_t* option = "lpf") |
virtual void | TObject::AppendPad(Option_t* option = "") |
virtual void | TObject::Browse(TBrowser* b) |
static TClass* | Class() |
virtual const char* | TObject::ClassName() const |
virtual void | Clear(Option_t* option = "")MENU |
virtual TObject* | TObject::Clone(const char* newname = "") const |
virtual Int_t | TObject::Compare(const TObject* obj) const |
virtual void | TPave::ConvertNDCtoPad() |
virtual void | Copy(TObject& obj) const |
virtual void | TObject::Delete(Option_t* option = "")MENU |
virtual void | DeleteEntry()MENU |
Int_t | TAttLine::DistancetoLine(Int_t px, Int_t py, Double_t xp1, Double_t yp1, Double_t xp2, Double_t yp2) |
virtual Int_t | TPave::DistancetoPrimitive(Int_t px, Int_t py) |
virtual void | Draw(Option_t* option = "") |
virtual TBox* | TBox::DrawBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2) |
virtual void | TObject::DrawClass() constMENU |
virtual TObject* | TObject::DrawClone(Option_t* option = "") constMENU |
virtual void | TPave::DrawPave(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize = 4, Option_t* option = "br") |
virtual void | TObject::Dump() constMENU |
virtual void | EditEntryAttFill() |
virtual void | EditEntryAttLine() |
virtual void | EditEntryAttMarker() |
virtual void | EditEntryAttText() |
virtual void | TObject::Error(const char* method, const char* msgfmt) const |
virtual void | TObject::Execute(const char* method, const char* params, Int_t* error = 0) |
virtual void | TObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0) |
virtual void | TPave::ExecuteEvent(Int_t event, Int_t px, Int_t py) |
virtual void | TObject::Fatal(const char* method, const char* msgfmt) const |
virtual TObject* | TObject::FindObject(const char* name) const |
virtual TObject* | TObject::FindObject(const TObject* obj) const |
virtual Rectangle_t | TBox::GetBBox() |
virtual TPoint | TBox::GetBBoxCenter() |
Int_t | TPave::GetBorderSize() const |
Float_t | GetColumnSeparation() const |
Double_t | TPave::GetCornerRadius() const |
virtual Option_t* | TObject::GetDrawOption() const |
static Long_t | TObject::GetDtorOnly() |
TLegendEntry* | GetEntry() const |
Float_t | GetEntrySeparation() const |
virtual Color_t | TAttFill::GetFillColor() const |
virtual Style_t | TAttFill::GetFillStyle() const |
virtual const char* | GetHeader() const |
virtual const char* | TObject::GetIconName() const |
virtual Color_t | TAttLine::GetLineColor() const |
virtual Style_t | TAttLine::GetLineStyle() const |
virtual Width_t | TAttLine::GetLineWidth() const |
TList* | GetListOfPrimitives() const |
Float_t | GetMargin() const |
virtual Option_t* | TPave::GetName() const |
Int_t | GetNColumns() const |
Int_t | GetNRows() const |
virtual char* | TObject::GetObjectInfo(Int_t px, Int_t py) const |
static Bool_t | TObject::GetObjectStat() |
virtual Option_t* | TPave::GetOption() const |
Int_t | TPave::GetShadowColor() const |
virtual Short_t | TAttText::GetTextAlign() const |
virtual Float_t | TAttText::GetTextAngle() const |
virtual Color_t | TAttText::GetTextColor() const |
virtual Font_t | TAttText::GetTextFont() const |
virtual Float_t | TAttText::GetTextSize() const |
virtual const char* | TObject::GetTitle() const |
virtual UInt_t | TObject::GetUniqueID() const |
Double_t | TBox::GetX1() const |
Double_t | TPave::GetX1NDC() const |
Double_t | TBox::GetX2() const |
Double_t | TPave::GetX2NDC() const |
Double_t | TBox::GetY1() const |
Double_t | TPave::GetY1NDC() const |
Double_t | TBox::GetY2() const |
Double_t | TPave::GetY2NDC() const |
virtual Bool_t | TObject::HandleTimer(TTimer* timer) |
virtual ULong_t | TPave::Hash() const |
virtual void | TBox::HideToolTip(Int_t event) |
virtual void | TObject::Info(const char* method, const char* msgfmt) const |
virtual Bool_t | TObject::InheritsFrom(const char* classname) const |
virtual Bool_t | TObject::InheritsFrom(const TClass* cl) const |
virtual void | InsertEntry(const char* objectName = "", const char* label = "", Option_t* option = "lpf")MENU |
virtual void | TObject::Inspect() constMENU |
void | TObject::InvertBit(UInt_t f) |
virtual TClass* | IsA() const |
Bool_t | TBox::IsBeingResized() const |
virtual Bool_t | TObject::IsEqual(const TObject* obj) const |
virtual Bool_t | TObject::IsFolder() const |
virtual Int_t | TBox::IsInside(Double_t x, Double_t y) const |
Bool_t | TObject::IsOnHeap() const |
virtual Bool_t | TPave::IsSortable() const |
virtual Bool_t | TAttFill::IsTransparent() const |
Bool_t | TObject::IsZombie() const |
virtual void | TPave::ls(Option_t* option = "") const |
void | TObject::MayNotUse(const char* method) const |
virtual void | TAttLine::Modify() |
virtual Bool_t | TObject::Notify() |
void | TObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const |
void | TObject::operator delete(void* ptr) |
void | TObject::operator delete(void* ptr, void* vp) |
void | TObject::operator delete[](void* ptr) |
void | TObject::operator delete[](void* ptr, void* vp) |
void* | TObject::operator new(size_t sz) |
void* | TObject::operator new(size_t sz, void* vp) |
void* | TObject::operator new[](size_t sz) |
void* | TObject::operator new[](size_t sz, void* vp) |
virtual void | Paint(Option_t* option = "") |
virtual void | TBox::PaintBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Option_t* option = "") |
virtual void | TPave::PaintPave(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize = 4, Option_t* option = "br") |
virtual void | TPave::PaintPaveArc(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize = 4, Option_t* option = "br") |
virtual void | PaintPrimitives() |
virtual void | TObject::Pop() |
virtual void | Print(Option_t* option = "") const |
virtual Int_t | TObject::Read(const char* name) |
virtual void | RecursiveRemove(TObject* obj) |
virtual void | TAttFill::ResetAttFill(Option_t* option = "") |
virtual void | TAttLine::ResetAttLine(Option_t* option = "") |
virtual void | TAttText::ResetAttText(Option_t* toption = "") |
void | TObject::ResetBit(UInt_t f) |
virtual void | TObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU |
virtual void | TAttFill::SaveFillAttributes(ostream& out, const char* name, Int_t coldef = 1, Int_t stydef = 1001) |
virtual void | TAttLine::SaveLineAttributes(ostream& out, const char* name, Int_t coldef = 1, Int_t stydef = 1, Int_t widdef = 1) |
virtual void | SavePrimitive(ostream& out, Option_t* option = "") |
virtual void | TAttText::SaveTextAttributes(ostream& out, const char* name, Int_t alidef = 12, Float_t angdef = 0, Int_t coldef = 1, Int_t fondef = 61, Float_t sizdef = 1) |
virtual void | TBox::SetBBoxCenter(const TPoint& p) |
virtual void | TBox::SetBBoxCenterX(const Int_t x) |
virtual void | TBox::SetBBoxCenterY(const Int_t y) |
virtual void | TBox::SetBBoxX1(const Int_t x) |
virtual void | TBox::SetBBoxX2(const Int_t x) |
virtual void | TBox::SetBBoxY1(const Int_t y) |
virtual void | TBox::SetBBoxY2(const Int_t y) |
void | TObject::SetBit(UInt_t f) |
void | TObject::SetBit(UInt_t f, Bool_t set) |
virtual void | TPave::SetBorderSize(Int_t bordersize = 4)MENU |
void | SetColumnSeparation(Float_t columnSeparation)MENU |
virtual void | TPave::SetCornerRadius(Double_t rad = 0.20000000000000001)MENU |
void | SetDefaults() |
virtual void | TObject::SetDrawOption(Option_t* option = "")MENU |
static void | TObject::SetDtorOnly(void* obj) |
virtual void | SetEntryLabel(const char* label)MENU |
virtual void | SetEntryOption(Option_t* option)MENU |
void | SetEntrySeparation(Float_t entryseparation)MENU |
virtual void | TAttFill::SetFillAttributes()MENU |
virtual void | TAttFill::SetFillColor(Color_t fcolor) |
virtual void | TAttFill::SetFillColorAlpha(Color_t fcolor, Float_t falpha) |
virtual void | TAttFill::SetFillStyle(Style_t fstyle) |
virtual void | SetHeader(const char* header = "")MENU |
virtual void | TAttLine::SetLineAttributes()MENU |
virtual void | TAttLine::SetLineColor(Color_t lcolor) |
virtual void | TAttLine::SetLineColorAlpha(Color_t lcolor, Float_t lalpha) |
virtual void | TAttLine::SetLineStyle(Style_t lstyle) |
virtual void | TAttLine::SetLineWidth(Width_t lwidth) |
void | SetMargin(Float_t margin)MENU |
virtual void | TPave::SetName(const char* name = "")MENU |
void | SetNColumns(Int_t nColumns)MENU |
static void | TObject::SetObjectStat(Bool_t stat) |
virtual void | TPave::SetOption(Option_t* option = "br") |
virtual void | TPave::SetShadowColor(Int_t color)MENU |
virtual void | TAttText::SetTextAlign(Short_t align = 11) |
virtual void | TAttText::SetTextAngle(Float_t tangle = 0)MENU |
virtual void | TAttText::SetTextAttributes()MENU |
virtual void | TAttText::SetTextColor(Color_t tcolor = 1) |
virtual void | TAttText::SetTextColorAlpha(Color_t tcolor, Float_t talpha) |
virtual void | TAttText::SetTextFont(Font_t tfont = 62) |
virtual void | TAttText::SetTextSize(Float_t tsize = 1) |
virtual void | TAttText::SetTextSizePixels(Int_t npixels) |
virtual void | TBox::SetToolTipText(const char* text, Long_t delayms = 1000) |
virtual void | TObject::SetUniqueID(UInt_t uid) |
virtual void | TPave::SetX1(Double_t x1) |
virtual void | TPave::SetX1NDC(Double_t x1) |
virtual void | TPave::SetX2(Double_t x2) |
virtual void | TPave::SetX2NDC(Double_t x2) |
virtual void | TPave::SetY1(Double_t y1) |
virtual void | TPave::SetY1NDC(Double_t y1) |
virtual void | TPave::SetY2(Double_t y2) |
virtual void | TPave::SetY2NDC(Double_t y2) |
virtual void | ShowMembers(TMemberInspector& insp) const |
virtual void | Streamer(TBuffer&) |
void | StreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b) |
virtual void | TObject::SysError(const char* method, const char* msgfmt) const |
Bool_t | TObject::TestBit(UInt_t f) const |
Int_t | TObject::TestBits(UInt_t f) const |
TLegend() | |
TLegend(const TLegend& legend) | |
TLegend(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char* header = "", Option_t* option = "brNDC") | |
virtual void | TObject::UseCurrentStyle() |
virtual void | TObject::Warning(const char* method, const char* msgfmt) const |
virtual Int_t | TObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) |
virtual Int_t | TObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const |
virtual void | TObject::DoError(int level, const char* location, const char* fmt, va_list va) const |
void | TObject::MakeZombie() |
TLegend& | operator=(const TLegend&) |
static TObject::(anonymous) | TObject::kBitMask | |
static TObject::EStatusBits | TObject::kCanDelete | |
static TBox::(anonymous) | TBox::kCannotMove | |
static TObject::EStatusBits | TObject::kCannotPick | |
static TObject::EStatusBits | TObject::kHasUUID | |
static TObject::EStatusBits | TObject::kInvalidObject | |
static TObject::(anonymous) | TObject::kIsOnHeap | |
static TObject::EStatusBits | TObject::kIsReferenced | |
static TObject::EStatusBits | TObject::kMustCleanup | |
static TPave::(anonymous) | TPave::kNameIsAction | |
static TObject::EStatusBits | TObject::kNoContextMenu | |
static TObject::(anonymous) | TObject::kNotDeleted | |
static TObject::EStatusBits | TObject::kObjInCanvas | |
static TObject::(anonymous) | TObject::kOverwrite | |
static TObject::(anonymous) | TObject::kSingleKey | |
static TObject::(anonymous) | TObject::kWriteDelete | |
static TObject::(anonymous) | TObject::kZombie |
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; }