Logo ROOT  
Reference Guide
legendautoplaced.C File Reference

Detailed Description

View in nbviewer Open in SWAN The legend can be placed automatically in the current pad in an empty space found at painting time.

The following example illustrate this facility. Only the width and height of the legend is specified in percentage of the pad size.

void legendautoplaced()
{
auto c4 = new TCanvas("c", "c", 600,500);
auto hpx = new TH1D("hpx","This is the hpx distribution",100,-4.,4.);
hpx->FillRandom("gaus", 50000);
hpx->Draw("E");
hpx->GetYaxis()->SetTitle("Y Axis title");
hpx->GetYaxis()->SetTitleOffset(1.3); hpx->GetYaxis()->CenterTitle(true);
hpx->GetXaxis()->SetTitle("X Axis title");
hpx->GetXaxis()->CenterTitle(true);
auto h1 = new TH1D("h1","A green histogram",100,-2.,2.);
h1->FillRandom("gaus", 10000);
h1->SetLineColor(kGreen);
h1->Draw("same");
auto g = new TGraph();
g->SetPoint(0, -3.5, 100 );
g->SetPoint(1, -3.0, 300 );
g->SetPoint(2, -2.0, 1000 );
g->SetPoint(3, 1.0, 800 );
g->SetPoint(4, 0.0, 200 );
g->SetPoint(5, 3.0, 200 );
g->SetPoint(6, 3.0, 700 );
g->Draw("L");
g->SetTitle("This is a TGraph");
g->SetLineColor(kRed);
g->SetFillColor(0);
// TPad::BuildLegend() default placement values are such that they trigger
// the automatic placement.
c4->BuildLegend();
}
Author
Olivier Couet

Definition in file legendautoplaced.C.

kGreen
@ kGreen
Definition: Rtypes.h:66
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:615
TAxis::CenterTitle
void CenterTitle(Bool_t center=kTRUE)
Center axis title.
Definition: TAxis.h:184
h1
TH1F * h1
Definition: legend1.C:5
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:318
kRed
@ kRed
Definition: Rtypes.h:66
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3444
TGraph
Definition: TGraph.h:41
TCanvas
Definition: TCanvas.h:23
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:317
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997
g
#define g(i)
Definition: RSha256.hxx:123