Logo ROOT   6.18/05
Reference Guide
legendautoplaced.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// The legend can be placed automatically in the current pad in an empty space
5/// found at painting time.
6///
7/// The following example illustrate this facility. Only the width and height of the
8/// legend is specified in percentage of the pad size.
9///
10/// \macro_image
11/// \macro_code
12///
13/// \author Olivier Couet
14
15void legendautoplaced()
16{
17 auto c4 = new TCanvas("c", "c", 600,500);
18 auto hpx = new TH1D("hpx","This is the hpx distribution",100,-4.,4.);
19 hpx->FillRandom("gaus", 50000);
20 hpx->Draw("E");
21 hpx->GetYaxis()->SetTitle("Y Axis title");
22 hpx->GetYaxis()->SetTitleOffset(1.3); hpx->GetYaxis()->CenterTitle(true);
23 hpx->GetXaxis()->SetTitle("X Axis title");
24 hpx->GetXaxis()->CenterTitle(true);
25
26 auto h1 = new TH1D("h1","A green histogram",100,-2.,2.);
27 h1->FillRandom("gaus", 10000);
29 h1->Draw("same");
30
31 auto g = new TGraph();
32 g->SetPoint(0, -3.5, 100 );
33 g->SetPoint(1, -3.0, 300 );
34 g->SetPoint(2, -2.0, 1000 );
35 g->SetPoint(3, 1.0, 800 );
36 g->SetPoint(4, 0.0, 200 );
37 g->SetPoint(5, 3.0, 200 );
38 g->SetPoint(6, 3.0, 700 );
39 g->Draw("L");
40 g->SetTitle("This is a TGraph");
41 g->SetLineColor(kRed);
42 g->SetFillColor(0);
43
44 // TPad::BuildLegend() default placement values are such that they trigger
45 // the automatic placement.
46 c4->BuildLegend();
47}
#define g(i)
Definition: RSha256.hxx:105
@ kRed
Definition: Rtypes.h:64
@ kGreen
Definition: Rtypes.h:64
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
The Canvas class.
Definition: TCanvas.h:31
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3428
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2981
TH1F * h1
Definition: legend1.C:5