A graph is an object made of two arrays X and Y, holding the x,y coordinates of n points.

Un-binned data

A graph or chart is a set of categorical variables, this is un-binned data.
A histogram is used for continuous data, where the bins represent ranges of data (binned data), see → Histograms.

→ Graph tutorials

Graph classes

ROOT provides numerous graph classes, of which the following are among the most used:

TGraph: A graph.

TGraphErrors: A graph with error bars.

TGraphBentErrors: A TGraph with bent, asymmetric error bars.

TGraphMultiErrors: A TGraph with asymmetric error bars and multiple y error dimensions.

TGraphPolar: A polar graph including error bars.

TGraphQQ: Draws quantile-quantile plots.

TMultiGraph: A collection of TGraph (or derived) objects.

TGraph2D: Graph made of three arrays X, Y and Z with the same number of points each.

TGraph2DErrors: A TGraph2D with error bars.

Working with graphs

ROOT supports the general case with non-equidistant points, and the special case with equidistant points.

Creating a graph

Use a graph constructor to create a graph object.


Arrays of coordinates are defined and then graph with the TGraph constructor is created.
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);

Drawing a graph


   const int 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);
   auto *gr1 = new TGraph (n, x, y);

Figure: Graph drawn with Draw().

Drawing options


The drawing options are not case sensitive.

The “drawing option” is the unique parameter of the TGraph::Draw() method. It specifies how the graph will be graphically rendered. For detailed information on the drawing options for graph classes, refer to TGraphPainter.


   const int 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);
   auto *gr1 = new TGraph (n, x, y);

Figure: Graph drawn with Draw(“AL*”).

Setting titles for a graph

Before giving the axis of a graph a title you need to draw the graph first.

You can set the title by getting the axis and calling the TGraph::SetTitle() method.

You can use the TAxis::CenterTitle method to center the title.


Assuming that n, x, and y are defined. Then the titles of the x and y axes are set as follows:

   root[] gr5 = new TGraph(n,x,y)
   root[] gr5->Draw()
   <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
   root[] gr5->Draw("ALP")
   root[] gr5->GetXaxis()->SetTitle("X-Axis")
   root[] gr5->GetYaxis()->SetTitle("Y-Axis")
   root[] gr5->GetXaxis()->CenterTitle()
   root[] gr5->GetYaxis()->CenterTitle()
   root[] gr5->Draw("ALP")

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.


   TCanvas *c1 = new TCanvas("c1","A Zoomed Graph",200,10,700,500);
   TH2F *hpx = new TH2F("hpx","Zoomed Graph Example",10,0,0.5,10,1.0,8.0);
   hpx->SetStats(kFALSE); // no statistics

   Int_t n = 10;
   Double_t x[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
   Double_t y[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

   TGraph *gr = new TGraph(n,x,y);

Figure: A zoomed graph.

Fitting graphs

  • Use the graph Fit() methods (for example TGraph::Fit()), for fitting graphs.

For more information on the Fit() method, → see Fitting histograms.

Graphs with error bars

A TGraphErrors is a TGraph with error bars.

The TGraphPainter class implements all drawing options for graphs with error bars.

TGraphErrors uses 4 parameters: X, Y (as in TGraph ), X-errors, and Y-errors (the size of the errors in the x and y direction).


   TCanvas *c1 = new TCanvas("c1","A Simple Graph with error bars",200,10,700,500);


// Create the coordinate arrays:
   const Int_t n = 10;
   Float_t x[n] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
   Float_t y[n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

// Create the error arrays:
   Float_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
   Float_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};

// Create the TGraphErrors and draw it.
   TGraphErrors *gr = new TGraphErrors(n,x,y,ex,ey);
   gr->SetTitle("TGraphErrors Example");


Figure: Graph with error bars.


A TMultiGraph is a collection of TGraph (or derived) objects.

The TMultiGraph owns the objects in the list. The drawing and fitting options are the same as for TGraph .


// Create the points:
   const 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};
   Double_t x2[n] = {-.12,.15,.35,.45,.6,.71,.8,.95,.99,1.05};
   Double_t y2[n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};

// Create the width of errors in x and y direction:
   Double_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
   Double_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};

// Create two graphs:
   TGraph *gr1 = new TGraph(n,x2,y2);
   TGraphErrors *gr2 = new TGraphErrors(n,x,y,ex,ey);

// Create a TMultiGraph and draw it:
   TMultiGraph *mg = new TMultiGraph();

Figure: Graph with error bars.


A TGraph2D graph is a graphics object that is made of three arrays X, Y and Z with the same number of points each.

The TGraph2D class has the following constructors:

  • With a dimension n of an array and three arrays x, y, and z (can be arrays of doubles, floats, or integers).
   TGraph2D *g = new TGraph2D(n,x,y,z);
  • With only the dimension n of an array.
   TGraph2D *g = new TGraph2D(n);
  • Internal arrays are filled with the SetPoint() method at the position i with the values x, y, z.
  • Without parameters. Use the SetPoint() method to fill the internal arrays.
   TGraph2D *g = new TGraph2D();
  • From a file. The arrays are read from the ASCII file, for example graph.dat according to a specified format. The format’s default value is %lg %lg %lg.
   TGraph2D *g = new TGraph2D("graph.dat");

Note that in any of last three cases, you can use the SetPoint() method to change a data point or to add a new one. If the data point index (i) is greater than the size of the internal arrays, they are automatically extended.

Drawing options

You can draw a TGraph2D with any drawing option valid for 2D histogram drawing. In this case, an intermediate 2D histogram is filled using the Delaunay triangles technique to interpolate the data set.

You can also use the following specific drawing options for TGraph2D graphs:

  • TRI: Delaunay triangles are drawn using the filled area. A hidden surface drawing technique is used. The surface is painted with the current fill area color. The edges of the triangles are painted with the current line color.
  • TRIW: Delaunay triangles are drawn as wire frames.
  • TRI1: Delaunay triangles are painted with color levels. The edges of the triangles are painted with the current line color.
  • TRI2: Delaunay triangles are painted with color levels.
  • P: Draws a marker at each vertex.
  • P0: Draws a circle at each vertex. Each circle background is white.


   TCanvas *c = new TCanvas("c","Graph2D example",0,0,700,600);
   Double_t x, y, z, P = 6.;
   Int_t np = 200;
   TGraph2D *dt = new TGraph2D();
   TRandom *r = new TRandom();
   for (Int_t N=0; N<np; N++) {
      x = 2*P*(r->Rndm(N))-P;
      y = 2*P*(r->Rndm(N))-P;
      z = (sin(x)/x)*(sin(y)/y)+0.2;

Figure: A TGraph2D with the drawing option surf1.