3The `
TGraph`
class in
PyROOT is
a versatile tool
for creating and visualizing 2D graphs with
points connected by straight lines. It is commonly used
for plotting scatter plots,
line graphs, and interpolated
data visualizations.
61. **Create and manipulate 2D graphs** with dynamic
data points.
72. **Customize
graph appearance:** markers, lines,
colors, and more.
83. **
Add labels, legends, and multiple datasets**
for comprehensive visualization.
94. **
Save graphs** in formats like .png, .pdf, and .root.
11Below are two examples:
121. **Basic Example:** Demonstrating
TGraph functionalities.
132. **Advanced Example:** Visualizing and comparing multiple datasets.
21# Create a canvas to draw the graph
22canvas =
ROOT.TCanvas(
"c1",
"TGraph Example", 800, 600)
24# Define the number of data points
26# Generate X values as a sequence from 0 to 9
27x = numpy.linspace(0, 9,
n)
28# Compute Y values as the square of X values
31# Create a TGraph object using the defined X and Y values
34# Get the current number of points in the graph
36# Add a new point to the graph at the specified index
39# Retrieve the coordinates of the 3rd point (index 2)
40x_val =
graph.GetPointX(2)
41y_val =
graph.GetPointY(2)
42print(
f"Point {2}: ({x_val}, {y_val})")
44# Customize graph appearance
45graph.SetTitle(
"Example Graph;X-axis Title;Y-axis Title")
46graph.SetMarkerStyle(21)
48graph.SetMarkerColor(4)
53# Draw the graph with axes ("A"), lines ("L"), and points ("P")
56# Save the graph as a PNG file
57canvas.SaveAs(
"tgraph_example.png")
60## Advanced Example: Comparing Two Datasets
61This example demonstrates how to visualize and compare multiple datasets
using `
TGraph`. The script customizes axes, labels and legends. The
output is
a comparative
graph saved
as a PDF.
67# Define benchmark data for Dataset 1 using NumPy arrays
68dataset1_x_values = numpy.array([32, 64, 128, 256, 512, 1024, 2048], dtype=
float)
69dataset1_y_values = numpy.array([2, 3, 7, 13, 26, 48, 100], dtype=
float)
71# Define benchmark data for Dataset 2 using NumPy arrays
72dataset2_x_values = numpy.array([32, 64, 128, 256, 512, 1024, 2048], dtype=
float)
73dataset2_y_values = numpy.array([2, 3, 7, 13, 26, 47, 92], dtype=
float)
75# Create TGraph objects for Dataset 1 and Dataset 2
76graph1 =
ROOT.TGraph(
len(dataset1_x_values), dataset1_x_values, dataset1_y_values)
77graph2 =
ROOT.TGraph(
len(dataset2_x_values), dataset2_x_values, dataset2_y_values)
79# Configure canvas dimensions for displaying the comparative plot
82# Left, Right, Bottom, Top margins
83canvas.SetMargin(0.08, 0.05, 0.15, 0.09)
85# Retrieve y-axis minimum and maximum values to set dynamic axis ranges for the plot
86ymin = graph1.GetHistogram().GetMinimum()
87ymax = graph1.GetHistogram().GetMaximum()
90graph1.SetTitle(
"TGraph dataset comparison plot title")
91graph1.GetXaxis().SetTitle(
"X Axis Title")
92graph1.GetXaxis().SetTitleSize(0.04)
93graph1.GetXaxis().SetTitleOffset(1.3)
94graph1.GetXaxis().SetLabelSize(0.03)
96# Hide default x-axis labels and ticks to customize axis appearance
97graph1.GetXaxis().SetLabelSize(0)
98graph1.GetXaxis().SetTickLength(0)
100graph1.GetYaxis().SetTitle(
"Y Axis Title")
101graph1.GetYaxis().SetTitleSize(0.04)
102graph1.GetYaxis().SetTitleOffset(0.7)
103graph1.GetYaxis().SetLabelSize(0.03)
104# Ensure y-axis includes zero
105graph1.GetYaxis().SetRangeUser(0,
ymax)
107graph1.SetMarkerColor(
ROOT.kAzure - 3)
108graph1.SetMarkerStyle(20)
109graph1.SetMarkerSize(2)
110graph1.SetLineColor(
ROOT.kAzure - 3)
111graph1.SetLineWidth(2)
116# Add custom x-axis labels and ticks to align with data points for better readability
121for i in range(
len(dataset1_x_values)):
122 # Get the
x-coordinate of the i-th
data point
123 x = graph1.GetPointX(i)
125 #
Add custom label
for the
x-axis
126 # Place the label slightly below the
x-axis
129 # Use
a standard
ROOT font
131 # Align
text to the left and bottom relative to the anchor point
133 #
Rotate the label 45 degrees
for better readability
136 # Keep
a reference to the label to ensure it persists
on the canvas
137 xaxislabels.append(label)
139 # Create
a tick at the
x-coordinate
143 # Keep
a reference to the tick to ensure it persists
on the canvas
144 xaxisticks.append(tick)
154graph2.
Draw(
"SAME LP")
156#
Add a legend to distinguish Dataset 1 and Dataset 2
158legend.
AddEntry(graph1,
"Dataset 1",
"lp")
159legend.
AddEntry(graph2,
"Dataset 2",
"lp")
164canvas.SaveAs(
"tgraph_comparison.pdf")
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t SetLineWidth
Option_t Option_t SetTextSize
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
Option_t Option_t SetLineColor
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t SetTextFont
Option_t Option_t SetMarkerStyle
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
Option_t Option_t TPoint TPoint const char text
A TGraph is an object made of two arrays X and Y with npoints each.
To draw Mathematical Formula.
This class displays a legend box (TPaveText) containing several legend entries.
Use the TLine constructor to create a simple line.
RooCmdArg Save(bool flag=true)
leg AddEntry(h1,"Histogram filled with random numbers","f")
Vector Rotate(const Vector &v, const RotationMatrix &rot)
rotation on a generic vector using a generic rotation class.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.