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
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
@ kWhite
Definition Rtypes.h:66
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:41
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:53
The Canvas class.
Definition TCanvas.h:23
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
void SetTitle(const char *title="") override
Set the title of the TNamed.
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
void Draw(Option_t *option="") override
Draw this box with its current attributes.
virtual void SetHeader(const char *header="", Option_t *option="")
virtual void SetBorderSize(Int_t bordersize=4)
Sets the border size of the TPave box and shadow.
Definition TPave.h:79
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16