# 6 Graphs

A graph is a graphics object made of two arrays X and Y, holding the x,y coordinates of n points. There are several graph classes; they are TGraph, TGraphErrors, TGraphAsymmErrors, and TMultiGraph.

## 6.1 TGraph

The TGraph class supports the general case with non-equidistant points, and the special case with equidistant points. Graphs are created with the TGraph constructor. First, we define the arrays of coordinates and then create the graph. The coordinates can be arrays of doubles or floats.

   Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0; i<n; i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}
TGraph *gr1 = new TGraph (n, x, y);

An alternative constructor takes only the number of points n. It is expected that the coordinates will be set later.

TGraph *gr2 = new TGraph(n);

The default constructor can also be used. Further calls to SetPoint() will extend the internal vectors.

TGraph *gr3 = new TGraph();

### 6.1.1 Graph Draw Options

The various drawing options for a graph are explained in TGraph::PaintGraph. They are:

• L” A simple poly-line between every points is drawn

• F” A fill area is drawn

• F1” Idem as “F” but fill area is no more repartee around X=0 or Y=0

• F2” draw a fill area poly line connecting the center of bins

• A” Axis are drawn around the graph

• C” A smooth curve is drawn

• *” A star is plotted at each point

• P” The current marker of the graph is plotted at each point

• B” A bar chart is drawn at each point

• []” Only the end vertical/horizontal lines of the error bars are drawn. This option only applies to the TGraphAsymmErrors.

• 1ylow = rwymin

The options are not case sensitive and they can be concatenated in most cases. Let us look at some examples.

#### 6.1.1.1 Continuous Line, Axis and Stars (AC*)

{
Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}

// create graph
TGraph *gr  = new TGraph(n,x,y);
TCanvas *c1 = new TCanvas("c1","Graph Draw Options",
200,10,600,400);

// draw the graph with axis, continuous line, and put
// a * at each point
gr->Draw("AC*");
}

#### 6.1.1.2 Bar Graphs (AB)

root[] TGraph *gr1 = new TGraph(n,x,y);
root[] gr1->SetFillColor(40);
root[] gr1->Draw("AB");

This code will only work if n, x, and y is defined. The previous example defines these. You need to set the fill color, because by default the fill color is white and will not be visible on a white canvas. You also need to give it an axis, or the bar chart will not be displayed properly.

#### 6.1.1.3 Filled Graphs (AF)

root[] TGraph *gr3 = new TGraph(n,x,y);
root[] gr3->SetFillColor(45);
root[] gr3->Draw("AF")

This code will only work if n, x, yare defined. The first example defines them. You need to set the fill color, because by default the fill color is white and will not be visible on a white canvas. You also need to give it an axis, or the filled polygon will not be displayed properly.

#### 6.1.1.4 Marker Options

{
Int_t n = 20;
Double_t x[n], y[n];

// build the arrays with the coordinate of points
for (Int_t i=0; i<n; i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}

// create graphs
TGraph *gr3  = new TGraph(n,x,y);
TCanvas *c1 = new TCanvas ("c1","Graph Draw Options",
200,10,600,400);

// draw the graph with the axis,contineous line, and put
// a marker using the graph's marker style at each point
gr3->SetMarkerStyle(21);
c1->cd(4);
gr3->Draw("APL");

// get the points in the graph and put them into an array
Double_t *nx = gr3->GetX();
Double_t *ny = gr3->GetY();

// create markers of different colors
for (Int_t j=2; j<n-1; j++) {
TMarker *m = new TMarker(nx[j], 0.5*ny[j], 22);
m->SetMarkerSize(2);
m->SetMarkerColor(31+j);
m->Draw();
}
}

## 6.2 Superimposing Two Graphs

To super impose two graphs you need to draw the axis only once, and leave out the “A” in the draw options for the second graph. Next is an example:

{
Int_t n = 20;
Double_t x[n], y[n], x1[n], y1[n];

// create a blue graph with a cos function
gr1->SetLineColor(4);
gr1->Draw("AC*");

// superimpose the second graph by leaving out the axis option "A"
gr2->SetLineWidth(3);
gr2->SetMarkerStyle(21);
gr2->SetLineColor(2);
gr2->Draw("CP");
}

## 6.3 Graphs with Error Bars

A TGraphErrors is a TGraph with error bars. The various draw format options of TGraphErrors::Paint() are derived from TGraph.

void TGraphErrors::Paint(Option_t *option)

In addition, it can be drawn with the “Z” option to leave off the small lines at the end of the error bars. If option contains “>”, an arrow is drawn at the end of the error bars. If option contains “|>”, a full arrow is drawn at the end of the error bars. The size of the arrow is set to 2/3 of the marker size.

The option “[]” is interesting to superimpose systematic errors on top of the graph with the statistical errors. When it is specified, only the end vertical/horizontal lines of the error bars are drawn.

To control the size of the lines at the end of the error bars (when option 1 is chosen) use SetEndErrorSize(np). By default np=1; np represents the number of pixels.

gStyle->SetEndErrorSize(np);

## 6.14 Zooming a Graph

To zoom a graph you can create a histogram with the desired axis range first. Draw the empty histogram and then draw the graph using the existing axis from the histogram.

{
c1 = new TCanvas("c1","A Zoomed Graph",200,10,700,500);
hpx = new TH2F("hpx","Zoomed Graph Example",10,0,0.5,10,1.0,8.0);
hpx->SetStats(kFALSE);   // no statistics
hpx->Draw();
Int_t n = 10;
Double_t x[n] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
Double_t y[n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
gr = new TGraph(n,x,y);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(20);
gr->Draw("LP");// and draw it without an axis
}

The next example is the same graph as above with a zoom in the x and y directions.

## 6.15 The User Interface for Graphs

The class TGraphEditor provides the user interface for setting the following graph attributes interactively:

• Title text entry field … sets the title of the graph.

• Shape radio button group - sets the graph shapes:

• No Line: draw unconnected points;
• Smooth Line: a smooth curve;
• Simple Line: a simple poly-line;
• Bart Chart: a bar chart at each point.
• Fill Area: a fill area is drawn.
• Show Marker - sets markers as visible or invisible.

• Exclusion Zone - specifies the exclusion zone parameters :

• ’+-‘ check button: sets on which side of the line the exclusion zone will be drawn;
• Width combo box: defines the width of the zone.