# TGraph visualization through PyROOT

**URL:** <https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435>\
**Category:** Newbie\
**Created:** [March 17, 2020, 12:41pm UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435 "2020-03-17T12:41:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unimelb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/dec6dc/32.png) [@unimelb](https://root-forum.cern.ch/u/unimelb)\
**Post date:** [March 17, 2020, 12:41pm UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/1 "2020-03-17T12:41:39Z")

</div>

Hi I have written a script in PyROOT through my python interpreter (PyCharm) which takes data from a TTree and creates a TGraph. The script runs smoothly however I no visualization of the graph appears, does anyone else have this issue? I have tried with another script found on the ROOT guides as well which results in the same issue. The script I found and have used for an example to get a better understanding og TGraphs through python is:

```auto
from __future__ import print_function
import ROOT
from math import sin
from array import array

c1 = ROOT.TCanvas( 'c1', 'A Simple Graph Example', 200, 10, 700, 500 )
c1.SetFillColor( 42 )
c1.SetGrid()
n = 20
x, y = array( 'd' ), array( 'd' )

for i in range( n ):
    x.append( 0.1*i )
    y.append( 10*sin( x[i]+0.2 ) )
    print(' i %i %f %f ' % (i,x[i],y[i]))
gr = ROOT.TGraph( n, x, y )
gr.SetLineColor( 2 )
gr.SetLineWidth( 4 )
gr.SetMarkerColor( 4 )
gr.SetMarkerStyle( 21 )
gr.SetTitle( 'a simple graph' )
gr.GetXaxis().SetTitle( 'X title' )
gr.GetYaxis().SetTitle( 'Y title' )
gr.Draw( 'ACP' )
# TCanvas.Update() draws the frame, after which one can change it
c1.Update()
c1.GetFrame().SetFillColor( 21 )
c1.GetFrame().SetBorderSize( 12 )
c1.Modified()
c1.Update()

```

Does anyone know why I don’t get a visualization of my graph when running this code?

Thank you for all help!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2020, 1:37pm UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/2 "2020-03-17T13:37:50Z")

</div>

Do you see the canvas ? an empty canvas ?  
as far as I can see what you are doing seems fine.  
@etejedor may know .

---

<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:** [March 17, 2020, 1:40pm UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/3 "2020-03-17T13:40:28Z")

</div>

Hi,

Can you run your script with `python -i name_of_script.py` ?

It might be that the Python interpreter process is finishing and your canvas goes away with it. The `-i` option keeps the Python process alive.

---

<div class="post-metadata">

**Author:** ![unimelb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/dec6dc/32.png) [@unimelb](https://root-forum.cern.ch/u/unimelb)\
**Post date:** [March 18, 2020, 12:58am UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/4 "2020-03-18T00:58:24Z")

</div>

Thank you for response. When I try to run the script this way I receive this response:

Traceback (most recent call last):  
File “graphs.py”, line 2, in   
import ROOT  
File “/Applications/root/lib/ROOT.py”, line 24, in   
import cppyy  
File “/Applications/root/lib/cppyy.py”, line 61, in   
import libPyROOT as \_backend  
ImportError: dynamic module does not define module export function (PyInit\_libPyROOT)

It may be worth mentioning that I am currently working on a project in physics and when my first issues with running PyROOT scripts arose my supervisor adviced me to run the script by:

which -a python2.7  
/usr/bin/python2.7 name\_of\_script.py

This method runs the script successfully however no canvas opens and no visualization appears. The only result I get from this is all the points corresponding to each i in the for loop printed onto the terminal.

---

<div class="post-metadata">

**Author:** ![unimelb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/dec6dc/32.png) [@unimelb](https://root-forum.cern.ch/u/unimelb)\
**Post date:** [March 18, 2020, 12:59am UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/5 "2020-03-18T00:59:34Z")

</div>

No, nothing appears except for all the coordinate points printed onto the terminal.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 18, 2020, 6:53am UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/6 "2020-03-18T06:53:35Z")

</div>

Note your macro is very simple and can be easily converted in C++. It is very similar to the first example given in the [TGraph reference guide](https://root.cern/doc/master/classTGraph.html). Unless you are obliged to use python that might be a solution (as your python installation does not seem to be optimal).

---

<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:** [March 18, 2020, 6:58am UTC](https://root-forum.cern.ch/t/tgraph-visualization-through-pyroot/38435/7 "2020-03-18T06:58:14Z")

</div>

> [@unimelb](#):
>
> Thank you for response. When I try to run the script this way I receive this response:
> 
> Traceback (most recent call last):  
> File “graphs.py”, line 2, in  
> import ROOT  
> File “/Applications/root/lib/ROOT.py”, line 24, in  
> import cppyy  
> File “/Applications/root/lib/cppyy.py”, line 61, in  
> import libPyROOT as \_backend  
> ImportError: dynamic module does not define module export function (PyInit\_libPyROOT)

The reason of this error is that you are using a ROOT installation that was compiled for Python3, but to run your script you are using Python2. Please try running your script with Python3.
