# Graphs

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.

Example

Arrays of coordinates are defined and then graph with the TGraph constructor is created.
The coordinates can be arrays of doubles or floats.

### Drawing a graph

Example

Figure: Graph drawn with Draw().

#### Drawing options

Note

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.

Example

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.

Example

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

### 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.

Example

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).

Example

Figure: Graph with error bars.

## TMultiGraph

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 .

Example

Figure: Graph with error bars.

## TGraph2D

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).
• With only the dimension n of an array.
• 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.
• 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.

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.

Example

Figure: A TGraph2D with the drawing option surf1.