Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist011_TH1_legend_autoplaced.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/// \date November 2024
14/// \author Olivier Couet
15
16void hist011_TH1_legend_autoplaced()
17{
18 auto *c4 = new TCanvas("c", "c", 600, 500);
19
20 // Create a dummy histogram
21 auto *hpx = new TH1D("hpx", "This is the hpx distribution", 100, -4., 4.);
22 hpx->FillRandom("gaus", 50000);
23 hpx->Draw("E");
24 hpx->GetYaxis()->SetTitle("Y Axis title");
25 hpx->GetYaxis()->SetTitleOffset(1.3);
26 hpx->GetYaxis()->CenterTitle(true);
27 hpx->GetXaxis()->SetTitle("X Axis title");
28 hpx->GetXaxis()->CenterTitle(true);
29
30 // Create another dummy histogram
31 auto *h1 = new TH1D("h1", "A green histogram", 100, -2., 2.);
32 h1->FillRandom("gaus", 10000);
34 h1->Draw("same");
35
36 // Create a dummy graph
37 auto *g = new TGraph();
38 g->SetPoint(0, -3.5, 100);
39 g->SetPoint(1, -3.0, 300);
40 g->SetPoint(2, -2.0, 1000);
41 g->SetPoint(3, 1.0, 800);
42 g->SetPoint(4, 0.0, 200);
43 g->SetPoint(5, 3.0, 200);
44 g->SetPoint(6, 3.0, 700);
45 g->Draw("L");
46 g->SetTitle("This is a TGraph");
47 g->SetLineColor(kRed);
48 g->SetFillColor(0);
49
50 // TPad::BuildLegend() default placement values are such that they trigger
51 // the automatic placement.
52 c4->BuildLegend();
53}
#define g(i)
Definition RSha256.hxx:105
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
The Canvas class.
Definition TCanvas.h:23
A TGraph is an 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:682
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3517
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3055
TH1F * h1
Definition legend1.C:5