A graph or chart is a set of categorical variables, this is un-binned data unlike a histogram which holds continuous data, where the bins represent ranges of data (binned data), see → Histograms.→ Graph tutorials
ROOT provides numerous graph classes:
TGraph: A graph.
TGraphErrors: A graph with symmetric error bars.
TGraphAsymmErrors: A graph with asymmetric error bars.
TGraphQQ: Draws quantile-quantile plots.
TGraph2D: A graph made of three arrays X, Y and Z with the same number of points each.
Working with TGraph
Creating a graph
- Use one of the TGraph constructor to create a graph object.
, and its derived classes, offers a wide variety of constructors.
can be created from an ASCII file, from a
, from a histogram etc …
The most common way being the creation from C++ arrays.
Arrays of coordinates are defined and then the graph is created with
The coordinates can be arrays of doubles or floats.
When the number of points to be stored in the graph is not known AddPoint can be used:
Drawing a graph
Figure: Graph drawn with Draw().
The “drawing option” is the unique parameter (case insensitive) of the TGraph::Draw() method. It specifies how the graph will be graphically rendered.
Figure: Graph drawn with Draw(“AL*”).
Setting titles for a graph
On the previous plot the graph title is “Graph”. It is the default graph title. The axis have no titles. Once a graph is created it is possible to change the graph and axis titles using TGraph::SetTitle() method.
You can use the TAxis::CenterTitle method to center the title. Note this acts on the axis which are available only once the graph has been drawn.
Figure: Graph with titles.
Zooming a graph
To zoom a graph you can first draw a frame with the wanted limits and then draw the graph in it
Figure: A zoomed graph.
See also “How to set ranges on axis”
- 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
Several drawing options are available for graphs with error bars.
Graphs with exclusion zone
It is possible to draw a graph with an exclusion zone.
When drawn with options
L one side of the graph is hatched.
Sometimes it is useful to draw the axis “reversed”. Ie: the X axis with its minimum on the right and its maximum on left of the plot and the Y axis its minimum on top and its maximum at the bottom of the plot. Some special drawing options allow to do that.
Logarithmic scales can be selected for the X and Y axis of a graph. But only the points building the graphs are changed into log scale. Not the lines connecting them.
Creating a TMultiGraph
- Use TMultiGraph::Add() to add a new graph to the list.
Figure: Graph with error bars.
Drawing a TMultiGraph
A specific option allows to draw the graph collection in 3D.
Zooming a TMultiGraph
Fitting a TMultiGraph
When several graphs are drawn on the same plot, it is possible to pick automatically the color of each graph in the current color palette.
allows to draw quantile-quantile plots. Such plots can
be drawn for two datasets, or for one dataset and a theoretical distribution function.
graph is a graphics object that is made of three
Z with the same number of points each.
Creating a TGraph2D
- Use one of the
TGraph2Dconstructors to create a 2D graph.
) have several
constructors. For instances
from three arrays
(can be arrays of doubles, floats, or integers),
from an ASCII file, or even
without parameter (in that case use the
SetPoint() method to
fill the internal arrays).
Drawing a TGraph2D
You can also use the specific TGraph2D drawing options
Figure: A TGraph2D with the drawing option TRI1 and P0.