Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gr111_legend.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// \preview Simple graph with legend. For more details on TLegend [see documentation](https://root.cern/doc/master/classTLegend.html)
5///
6/// \macro_image
7/// \macro_code
8/// \date 25/11/2024
9/// \author Emanuele Chiamulera
10
11void gr111_legend() {
12
13 auto c = new TCanvas("c","", 900, 800);
14 c->SetGrid();
15
16 const Int_t n = 5;
17 Double_t x[n] = {-3.00, -2.68, -2.36, -2.05, -1.73};
18 Double_t y1[n] = {-0.98, -0.89, -0.71, -0.46, -0.16};
19 Double_t y2[n] = {-2.98, -2.89, -2.71, -2.46, -2.16};
20
21
22 TGraph *gr1 = new TGraph(n, x, y1); //Create the graph
23 gr1->SetLineColor(2);
24 gr1->SetLineWidth(4);
25 gr1->SetMarkerColor(4);
26 gr1->SetMarkerStyle(21);
27 gr1->SetTitle("Graph with legend");
28
29 TLegend *legend = new TLegend(0.1,0.7,0.38,0.9); //Create the TLegend object and define it's position
30 legend->SetHeader("Legend title", "C"); //"C" Center alignment for the header ("L" Left and "R" Right)
31 legend->SetFillColor(kWhite);
32 legend->SetBorderSize(1);
33 legend->SetTextSize(0.04);
34 legend->AddEntry(gr1, "Data points", "lp"); // "p" for point marker, "l" for line, "e" for error bars if TGraphError is used.
35
36 gr1->Draw("");
37
38 // Draw the legend
39 legend->Draw();
40}
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
@ kWhite
Definition Rtypes.h:65
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
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
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16