Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gr001_simple.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_graphs
3## \notebook
4## \preview This tutorial demonstrates how to create simple graphs in ROOT. The example is divided into two sections:
5## - The first section plots data generated from arrays.
6## - The second section plots data read from a text file.
7##
8## \macro_image
9## \macro_code
10##
11## \author Rene Brun, Jamie Gooding
12
13import re
14
15import numpy as np
16import ROOT
17
18c1 = ROOT.TCanvas("c1", "Two simple graphs", 200, 10, 700, 500)
20 2, 1
21) # Dividing the canvas in subpads for distinguishing the two examples, [See documentation](https://root.cern/doc/master/classTCanvas.html)
22
23# First Example (Available data)
24c1.cd(1)
25
26n = 20
27x = np.linspace(0, n - 1, n)
28y = 10 * np.sin(x + 0.2)
29
30gr1 = ROOT.TGraph(n, x, y) # Create a TGraph object, storing the number of data n and the x, y variables
31
32# Set the color, width and style for the markers and line
33
38gr1.SetTitle("Graph from available data") # Choose title for the graph
39gr1.GetXaxis().SetTitle("X title") # Choose title for the axis
40gr1.GetYaxis().SetTitle("Y title")
41
42# Uncomment the following line to set a custom range for the x-axis (respectively for the y-axis):
43# gr1.GetXaxis().SetRangeUser(0, 1.8)
44
46 "ACP"
47) # "A" draw axes, "C" = draw a smooth line through the markers (optional) and "P" = draw markers for data points
48# Optional customization can be done on a ROOT interactive session
49
50
51# SECOND EXAMPLE (Data stored in a text file)
52c1.cd(2)
53
54w = np.array([])
55z = np.array([])
56
57# Open the data file
58with open(f"{ROOT.gROOT.GetTutorialDir()}/visualisation/graphs/data_basic.txt") as file: # Open the data file
59 for line in file:
60 w_str, z_str = re.split(r"\s+", line)[:2]
61 w = np.append(w, float(w_str))
62 z = np.append(z, float(z_str))
63
64m = len(w)
65
66gr2 = ROOT.TGraph(m, w, z) # Create a TGraph object for the file data
71gr2.SetTitle("Graph from data file")
72gr2.GetXaxis().SetTitle("W title")
73gr2.GetYaxis().SetTitle("Z title")
74
75gr2.Draw("ACP")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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