The graph painter class.
Implements all graphs' drawing's options.
Introduction
Graphs are drawn via the painter TGraphPainter
class. This class implements techniques needed to display the various kind of graphs i.e.: TGraph
, TGraphErrors
, TGraphBentErrors
and TGraphAsymmErrors
.
To draw a graph graph
it's enough to do:
graph->Draw("AL");
The option AL
in the Draw()
method means:
- The axis should be drawn (option
A
),
The graph should be drawn as a simple line (option L
).
By default a graph is drawn in the current pad in the current coordinate system. To define a suitable coordinate system and draw the axis the option A
must be specified.
TGraphPainter
offers many options to paint the various kind of graphs.
It is separated from the graph classes so that one can have graphs without the graphics overhead, for example in a batch program.
When a displayed graph is modified, there is no need to call Draw()
again; the image will be refreshed the next time the pad will be updated. A pad is updated after one of these three actions:
- a carriage return on the ROOT command line,
- a click inside the pad,
- a call to
TPad::Update
.
Graphs' plotting options
Graphs can be drawn with the following options:
Option | Description |
"A" | Axis are drawn around the graph |
"I" | Combine with option 'A' it draws invisible axis |
"L" | A simple polyline is drawn |
"F" | A fill area is drawn ('CF' draw a smoothed fill area) |
"C" | A smooth Curve is drawn |
"*" | A Star is plotted at each point |
"P" | The current marker is plotted at each point |
"B" | A Bar chart is drawn |
"1" | When a graph is drawn as a bar chart, this option makes the bars start from the bottom of the pad. By default they start at 0. |
"X+" | The X-axis is drawn on the top side of the plot. |
"Y+" | The Y-axis is drawn on the right side of the plot. |
"PFC" | Palette Fill Color: graph's fill color is taken in the current palette. |
"PLC" | Palette Line Color: graph's line color is taken in the current palette. |
"PMC" | Palette Marker Color: graph's marker color is taken in the current palette. |
"RX" | Reverse the X axis. |
"RY" | Reverse the Y axis. |
Drawing options can be combined. In the following example the graph is drawn as a smooth curve (option "C") with markers (option "P") and with axes (option "A").
{
auto c1 =
new TCanvas(
"c1",
"c1",200,10,600,400);
}
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);
}
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
virtual void SetLineColor(Color_t lcolor)
Set the line color.
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
A Graph is a graphics object made of two arrays X and Y with npoints each.
virtual void SetTitle(const char *title="")
Set graph title.
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
TAxis * GetXaxis() const
Get x axis of the graph.
TAxis * GetYaxis() const
Get y axis of the graph.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
The following macro shows the option "B" usage. It can be combined with the option "1".
{
auto c47 =
new TCanvas(
"c47",
"c47",200,10,600,400);
c47->Divide(1,2);
y[i] = 10*
sin(
x[i]+0.2)-6;
}
}
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Exclusion graphs
When a graph is painted with the option C
or L
it is possible to draw a filled area on one side of the line. This is useful to show exclusion zones.
This drawing mode is activated when the absolute value of the graph line width (set by SetLineWidth()
) is greater than 99. In that case the line width number is interpreted as:
100*ff+ll = ffll
- The two digits number
ll
represent the normal line width
- The two digits number
ff
represent the filled area width.
- The sign of "ffll" allows to flip the filled area from one side of the line to the other.
The current fill area attributes are used to draw the hatched zone.
mg->SetTitle(
"Exclusion graphs");
Double_t xvalues1[
n], xvalues2[
n], xvalues3[
n], yvalues1[
n], yvalues2[
n], yvalues3[
n];
xvalues1[i] = i*0.1;
xvalues2[i] = xvalues1[i];
xvalues3[i] = xvalues1[i]+.5;
yvalues1[i] = 10*
sin(xvalues1[i]);
yvalues2[i] = 10*
cos(xvalues1[i]);
yvalues3[i] = 10*
sin(xvalues1[i])-2;
}
}
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
A TMultiGraph is a collection of TGraph (or derived) objects.
static constexpr double mg
Graphs with error bars
Three classes are available to handle graphs with error bars: TGraphErrors
, TGraphAsymmErrors
and TGraphBentErrors
. The following drawing options are specific to graphs with error bars:
Option | Description |
"Z" | Do not draw small horizontal and vertical lines the end of the error bars. Without "Z", the default is to draw these. |
">" | An arrow is drawn at the end of the error bars. The size of the arrow is set to 2/3 of the marker size. |
"|>" | A filled arrow is drawn at the end of the error bars. The size of the arrow is set to 2/3 of the marker size. |
"X" | Do not draw error bars. By default, graph classes that have errors are drawn with the errors (TGraph itself has no errors, and so this option has no effect.) |
"||" | Draw only the small vertical/horizontal lines at the ends of the error bars, without drawing the bars themselves. This option is interesting to superimpose statistical-only errors on top of a graph with statistical+systematic errors. |
"[]" | Does the same as option "||" except that it draws additional marks at the ends of the small vertical/horizontal lines. It makes plots less ambiguous in case several graphs are drawn on the same picture. |
"0" | By default, when a data point is outside the visible range along the Y axis, the error bars are not drawn. This option forces error bars' drawing for the data points outside the visible range along the Y axis (see example below). |
"2" | Error rectangles are drawn. |
"3" | A filled area is drawn through the end points of the vertical error bars. |
"4" | A smoothed filled area is drawn through the end points of the vertical error bars. |
"5" | Error rectangles are drawn like option "2". In addition the contour line around the boxes is drawn. This can be useful when boxes' fill colors are very light or in gray scale mode. |
gStyle->SetErrorX(dx)
controls the size of the error along x. dx = 0
removes the error along x.
gStyle->SetEndErrorSize(np)
controls the size of the lines at the end of the error bars (when option 1 is used). By default np=1
. (np represents the number of pixels).
A TGraphErrors
is a TGraph
with error bars. The errors are defined along X and Y and are symmetric: The left and right errors are the same along X and the bottom and up errors are the same along Y.
{
auto c4 =
new TCanvas(
"c4",
"c4",200,10,600,400);
double x[] = {0, 1, 2, 3, 4};
double y[] = {0, 2, 4, 1, 3};
double ex[] = {0.1, 0.2, 0.3, 0.4, 0.5};
double ey[] = {1, 0.5, 1, 0.5, 1};
}
A TGraphErrors is a TGraph with error bars.
The option "0" shows the error bars for data points outside range.
{
auto c48 =
new TCanvas(
"c48",
"c48",200,10,600,400);
float err_x[] = {0,0,0};
float err_y[] = {5,5,5};
c48->Divide(2,1);
c48->cd(1);
gPad->DrawFrame(0,0,4,8); tg.Draw(
"PC");
c48->cd(2);
gPad->DrawFrame(0,0,4,8); tg.Draw(
"0PC");
}
The option "3" shows the errors as a band.
{
auto c41 =
new TCanvas(
"c41",
"c41",200,10,600,400);
double x[] = {0, 1, 2, 3, 4};
double y[] = {0, 2, 4, 1, 3};
double ex[] = {0.1, 0.2, 0.3, 0.4, 0.5};
double ey[] = {1, 0.5, 1, 0.5, 1};
}
The option "4" is similar to the option "3" except that the band is smoothed. As the following picture shows, this option should be used carefully because the smoothing algorithm may show some (huge) "bouncing" effects. In some cases it looks nicer than option "3" (because it is smooth) but it can be misleading.
{
auto c42 =
new TCanvas(
"c42",
"c42",200,10,600,400);
double x[] = {0, 1, 2, 3, 4};
double y[] = {0, 2, 4, 1, 3};
double ex[] = {0.1, 0.2, 0.3, 0.4, 0.5};
double ey[] = {1, 0.5, 1, 0.5, 1};
}
The following example shows how the option "[]" can be used to superimpose systematic errors on top of a graph with statistical errors.
{
auto c43 =
new TCanvas(
"c43",
"c43",200,10,600,400);
c43->DrawFrame(0., -0.5, 6., 2);
double x[5] = {1, 2, 3, 4, 5};
double zero[5] = {0, 0, 0, 0, 0};
double py1[5] = {1.2, 1.15, 1.19, 0.9, 1.4};
double ey_stat1[5] = {0.2, 0.18, 0.17, 0.2, 0.4};
double ey_sys1[5] = {0.5, 0.71, 0.76, 0.5, 0.45};
double y2[5] = {0.25, 0.18, 0.29, 0.2, 0.21};
double ey_stat2[5] = {0.2, 0.18, 0.17, 0.2, 0.4};
double ey_sys2[5] = {0.63, 0.19, 0.7, 0.2, 0.7};
}
A TGraphAsymmErrors
is like a TGraphErrors
but the errors defined along X and Y are not symmetric: The left and right errors are different along X and the bottom and up errors are different along Y.
{
auto c44 =
new TCanvas(
"c44",
"c44",200,10,600,400);
double ax[] = {0, 1, 2, 3, 4};
double ay[] = {0, 2, 4, 1, 3};
double aexl[] = {0.1, 0.2, 0.3, 0.4, 0.5};
double aexh[] = {0.5, 0.4, 0.3, 0.2, 0.1};
double aeyl[] = {1, 0.5, 1, 0.5, 1};
double aeyh[] = {0.5, 1, 0.5, 1, 0.5};
}
TGraph with asymmetric error bars.
A TGraphBentErrors
is like a TGraphAsymmErrors
. An extra parameter allows to bend the error bars to better see them when several graphs are drawn on the same plot.
{
auto c45 =
new TCanvas(
"c45",
"c45",200,10,600,400);
Double_t x[
n] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
Double_t y[
n] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
Double_t exl[
n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
Double_t eyl[
n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};
Double_t exh[
n] = {.02,.08,.05,.05,.03,.03,.04,.05,.06,.03};
Double_t eyh[
n] = {.6,.5,.4,.3,.2,.2,.3,.4,.5,.6};
Double_t exld[
n] = {.0,.0,.0,.0,.0,.0,.0,.0,.0,.0};
Double_t eyld[
n] = {.0,.0,.05,.0,.0,.0,.0,.0,.0,.0};
Double_t exhd[
n] = {.0,.0,.0,.0,.0,.0,.0,.0,.0,.0};
Double_t eyhd[
n] = {.0,.0,.0,.0,.0,.0,.0,.0,.05,.0};
TGraphBentErrors *
gr =
new TGraphBentErrors(
n,
x,
y,exl,exh,eyl,eyh,exld,exhd,eyld,eyhd);
}
A TGraphBentErrors is a TGraph with bent, assymetric error bars.
The drawing options for the polar graphs are the following:
Option | Description |
"O" | Polar labels are drawn orthogonally to the polargram radius. |
"P" | Polymarker are drawn at each point position. |
"E" | Draw error bars. |
"F" | Draw fill area (closed polygon). |
"A" | Force axis redrawing even if a polargram already exists. |
"N" | Disable the display of the polar labels. |
{
auto c46 =
new TCanvas(
"c46",
"c46",500,500);
c46->Update();
}
TGraphPolargram * GetPolargram()
void Draw(Option_t *options="")
Draw TGraphPolar.
void SetToRadian()
The Polar circle is labelled using radian.
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Colors automatically picked in palette
- Since
- ROOT version 6.09/01
When several graphs are painted in the same canvas or when a multi-graph is drawn, it might be useful to have an easy and automatic way to choose their color. The simplest way is to pick colors in the current active color palette. Palette coloring for histogram is activated thanks to the options PFC
(Palette Fill Color), PLC
(Palette Line Color) and PMC
(Palette Marker Color). When one of these options is given to TGraph::Draw
the graph get its color from the current color palette defined by gStyle->SetPalette(…)
. The color is determined according to the number of objects having palette coloring in the current pad.
void graphpalettecolor () {
double x[5] = {1,2,3,4,5};
double y1[5] = {1.0,2.0,1.0,2.5,3.0};
double y2[5] = {1.1,2.1,1.1,2.6,3.1};
double y3[5] = {1.2,2.2,1.2,2.7,3.2};
double y4[5] = {1.3,2.3,1.3,2.8,3.3};
double y5[5] = {1.4,2.4,1.4,2.9,3.4};
}
R__EXTERN TStyle * gStyle
void SetOptTitle(Int_t tit=1)
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
void multigraphpalettecolor()
{
auto gr4 =
new TGraph(); gr4->SetMarkerStyle(24);
for (int i=0; i<=100; i++) {
}
}
Reverse graphs' axis
- Since
- ROOT version 6.09/03
When a TGraph is drawn, the X-axis is drawn with increasing values from left to right and the Y-axis from bottom to top. The two options RX
and RY
allow to change this order. The option RX
allows to draw the X-axis with increasing values from right to left and the RY
option allows to draw the Y-axis with increasing values from top to bottom. The following example illustrate how to use these options.
{
g->SetTitle(
"Simple Graph");
g->SetPointError(0,1.,2.);
g->GetXaxis()->SetNdivisions(520);
c->cd(1);
gPad->SetGrid(1,1);
c->cd(2);
gPad->SetGrid(1,1);
}
Graphs in logarithmic scale
Like histograms, graphs can be drawn in logarithmic scale along X and Y. When a pad is set to logarithmic scale with TPad::SetLogx() and/or with TPad::SetLogy() the points building the graph are converted into logarithmic scale. But only the points not the lines connecting them which stay linear. This can be clearly seen on the following example:
{
g->SetPoint(1,845,0.06504);
g->SetPoint(2,
xmax,0.008);
}
auto cv =
new TCanvas(
"cv",
"cv",800,600);
cv->SetLogy();
cv->SetGridx();
cv->SetGridy();
}
Highlight mode for graph
- Since
- ROOT version 6.15/01
Highlight mode
Highlight mode is implemented for TGraph
(and for TH1
) class. When highlight mode is on, mouse movement over the point will be represented graphically. Point will be highlighted as "point circle" (presented by marker object). Moreover, any highlight (change of point) emits signal TCanvas::Highlighted()
which allows the user to react and call their own function. For a better understanding please see also the tutorials $ROOTSYS/tutorials/graphs/hlGraph*.C
files.
Highlight mode is switched on/off by TGraph::SetHighlight()
function or interactively from TGraph
context menu. TGraph::IsHighlight()
to verify whether the highlight mode enabled or disabled, default it is disabled.
root [0] .x $ROOTSYS/tutorials/graphs/gerrors2.C
root [1]
Highlight mode for graph
See how it is used highlight mode and user function (is fully equivalent as for histogram).
NOTE all parameters of user function are taken from
void TCanvas::Highlighted(TVirtualPad *pad, TObject *obj, Int_t x, Int_t y)
pad
is pointer to pad with highlighted graph
obj
is pointer to highlighted graph
x
is highlighted x-th (i-th) point for graph
y
not in use (only for 2D histogram)
For more complex demo please see for example $ROOTSYS/tutorials/math/hlquantiles.C
file.
Definition at line 29 of file TGraphPainter.h.