# ROOT Python Plotting TTree with TGraph Formatting Issue

**URL:** <https://root-forum.cern.ch/t/root-python-plotting-ttree-with-tgraph-formatting-issue/33685>\
**Category:** Newbie\
**Created:** [April 19, 2019, 8:06pm UTC](https://root-forum.cern.ch/t/root-python-plotting-ttree-with-tgraph-formatting-issue/33685 "2019-04-19T20:06:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Food](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@Food](https://root-forum.cern.ch/u/Food)\
**Post date:** [April 19, 2019, 8:06pm UTC](https://root-forum.cern.ch/t/root-python-plotting-ttree-with-tgraph-formatting-issue/33685/1 "2019-04-19T20:06:58Z")

</div>

[dt-stormer-verlet.root](https://root-forum.cern.ch/uploads/default/original/3X/d/c/dc9e1e2447171884b3e0f9cf8c07605e1720414f.root) (708.2 KB)  
[readroot.py](https://root-forum.cern.ch/uploads/default/original/3X/7/8/788a7e8c8f8550bfa29baae17f74c5e98c468719.py) (1.5 KB)  
I’m trying to format a TTree graph that I’m piping to a TGraph object (g1) in python 2.7.

Here’s a look at the libraries.

```auto
import ROOT
from ROOT import TTree,TFile,gROOT
from ROOT import TCanvas,TGraph,TPad,TBrowser

```

I read in the root tree file.

```auto
file=TFile("dt-stormer-verlet.root", "read");
tree=file.Get("time-sequence")

```

Then I try to plot the two branches (box.atoms.KE\_PE\_V\_AVG:box.atoms.dt)

```auto
c=TCanvas("c", "canvas", 800, 800)
c.GetFrame().SetBorderSize(12)
c.cd()

tree.SetLineStyle(3)
tree.SetLineColor(2)
tree.SetLineWidth(4)
tree.SetMarkerColor(1)
tree.SetMarkerStyle(2)

tree.Draw("box.atoms.KE_PE_V_AVG:box.atoms.dt>>g1","dt<2","")
g1=TGraph(tree.GetSelectedRows(),tree.GetV2(),tree.GetV1())
g1.SetTitle( 'a simple graph' )
g1.Draw("")
g1.SetTitle( 'a simple graph' )
g1.GetXaxis().SetTitle('X title')
g1.GetYaxis().SetTitle("Y title")

c.SetGrid()
c.Modified()
c.Update()
text=raw_input()

```

The issue I am having is the title, and axis labels are not printing on the canvas and I am not sure why. Suggestions, questions, solutions would be greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Food](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/e79b87/32.png) [@Food](https://root-forum.cern.ch/u/Food)\
**Post date:** [April 20, 2019, 8:05pm UTC](https://root-forum.cern.ch/t/root-python-plotting-ttree-with-tgraph-formatting-issue/33685/2 "2019-04-20T20:05:51Z")

</div>

So I ended up solving this on my own. I was not sure why the X and Y axis titles were not printing on the TGraph. Here is the solution:

There were two issues.

1.) the `tree` object will not filled or not actually being pointed to. So to access branches of the tree (that are `std::vectors`) I need to call `tree` in a for loop (I am not sure why this works).

```auto
file=TFile("dt-stormer-verlet.root", "read");
tree=file.Get("time-sequence")

for i, event in enumerate(tree):
    print("{}".format(event))

```

2.) I wasn’t using the Draw() module correctly. The order matters, here’s the order.

```auto
tree.Draw("box.atoms.KE_PE_V_AVG:box.atoms.dt","dt<2","L")
g1=TGraph(tree.GetSelectedRows(),tree.GetV2(), tree.GetV1())
g1.SetTitle('')
g1.GetXaxis().SetTitle('Omega_dt')
g1.GetYaxis().SetTitle("Average Total Energy")
g1.Draw("A*L")

```

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 23, 2019, 10:10am UTC](https://root-forum.cern.ch/t/root-python-plotting-ttree-with-tgraph-formatting-issue/33685/3 "2019-04-23T10:10:38Z")

</div>

Dear @Food ,  
Thank you for posting the solution to your problem so that others may benefit from it. Glad it is working now.  
Best,  
Enric
