Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gr111_legend.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Simple graph with legend.

For more details on TLegend see documentation

void gr111_legend() {
auto c = new TCanvas("c","", 900, 800);
c->SetGrid();
const Int_t n = 5;
Double_t x[n] = {-3.00, -2.68, -2.36, -2.05, -1.73};
Double_t y1[n] = {-0.98, -0.89, -0.71, -0.46, -0.16};
Double_t y2[n] = {-2.98, -2.89, -2.71, -2.46, -2.16};
TGraph *gr1 = new TGraph(n, x, y1); //Create the graph
gr1->SetLineColor(2);
gr1->SetLineWidth(4);
gr1->SetMarkerColor(4);
gr1->SetMarkerStyle(21);
gr1->SetTitle("Graph with legend");
TLegend *legend = new TLegend(0.1,0.7,0.38,0.9); //Create the TLegend object and define it's position
legend->SetHeader("Legend title", "C"); //"C" Center alignment for the header ("L" Left and "R" Right)
legend->SetFillColor(kWhite);
legend->SetBorderSize(1);
legend->SetTextSize(0.04);
legend->AddEntry(gr1, "Data points", "lp"); // "p" for point marker, "l" for line, "e" for error bars if TGraphError is used.
gr1->Draw("");
// Draw the legend
legend->Draw();
}
#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
Date
25/11/2024
Author
Emanuele Chiamulera

Definition in file gr111_legend.C.