Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
15 void 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);
28  h1->SetLineColor(kGreen);
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 }