61. **Create and manipulate 2D graphs** with dynamic data
points.
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
37graph.SetPoint(
n, 10, 100)
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
80width, height = 1920, 1080
81canvas = ROOT.TCanvas("c",
"Datasets Comparison", width, height)
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")
Use the TLine constructor to create a simple line.
RooCmdArg Save(bool flag=true)
Vector Rotate(const Vector &v, double alpha, const Vector &axis)
rotation along a custom axis for a generic vector by an Angle alpha (in rad) returning a new vector.
leg AddEntry(h1,"Histogram filled with random numbers","f")
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.