Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gr007_multigraph.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_graphs
3## \notebook
4## \preview TMultiGraph is used to combine multiple graphs into one plot.
5## Allowing to overlay different graphs can be useful for comparing different datasets
6## or for plotting multiple related graphs on the same canvas.
7##
8## \macro_image
9## \macro_code
10##
11## \author Rene Brun, Jamie Gooding
12
13import numpy as np
14import ROOT
15
17c1 = ROOT.TCanvas("c1", "multigraph", 700, 500)
19
20# Initialize a TMultiGraph to hold multiple graphs
21# This ensures the entire dataset from all added graphs is visible without manual range adjustments.
23
24# Create first graph
25n1 = 10
26px1 = np.array([-0.1, 0.05, 0.25, 0.35, 0.5, 0.61, 0.7, 0.85, 0.89, 0.95])
27py1 = np.array([-1, 2.9, 5.6, 7.4, 9, 9.6, 8.7, 6.3, 4.5, 1])
28ex1 = np.array([0.05, 0.1, 0.07, 0.07, 0.04, 0.05, 0.06, 0.07, 0.08, 0.05])
29ey1 = np.array([0.8, 0.7, 0.6, 0.5, 0.4, 0.4, 0.5, 0.6, 0.7, 0.8])
30gr1 = ROOT.TGraphErrors(n1, px1, py1, ex1, ey1)
33
34gr1.Fit("gaus", "q")
35func1 = gr1.GetListOfFunctions().FindObject("gaus")
37
38# Add the first graph to the multigraph
39mg.Add(gr1)
40
41# Create second graph
42n2 = 10
43x2 = np.array([-0.28, 0.005, 0.19, 0.29, 0.45, 0.56, 0.65, 0.80, 0.90, 1.01])
44y2 = np.array([2.1, 3.86, 7, 9, 10, 10.55, 9.64, 7.26, 5.42, 2])
45ex2 = np.array([0.04, 0.12, 0.08, 0.06, 0.05, 0.04, 0.07, 0.06, 0.08, 0.04])
46ey2 = np.array([0.6, 0.8, 0.7, 0.4, 0.3, 0.3, 0.4, 0.5, 0.6, 0.7])
47gr2 = ROOT.TGraphErrors(n2, x2, y2, ex2, ey2)
50
51gr2.Fit("pol5", "q")
52func2 = gr2.GetListOfFunctions().FindObject("pol5")
55
56# Add the second graph to the multigraph
57mg.Add(gr2)
58
59mg.Draw("ap")
60
61# Force drawing of canvas to generate the fit TPaveStats
63
64stats1 = gr1.GetListOfFunctions().FindObject("stats")
65stats2 = gr2.GetListOfFunctions().FindObject("stats")
66
67if stats1 and stats2:
70 stats1.SetX1NDC(0.12)
71 stats1.SetX2NDC(0.32)
72 stats1.SetY1NDC(0.82)
73 stats2.SetX1NDC(0.72)
74 stats2.SetX2NDC(0.92)
75 stats2.SetY1NDC(0.75)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.