Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist011_TH1_legend_autoplaced.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.

{
auto *c4 = new TCanvas("c", "c", 600, 500);
// Create a dummy histogram
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);
// Create another dummy histogram
auto *h1 = new TH1D("h1", "A green histogram", 100, -2., 2.);
h1->FillRandom("gaus", 10000);
h1->Draw("same");
// Create a dummy graph
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();
}
#define g(i)
Definition RSha256.hxx:105
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:693
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3499
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
TH1F * h1
Definition legend1.C:5
Date
November 2024
Author
Olivier Couet

Definition in file hist011_TH1_legend_autoplaced.C.