# Issue converting argument for TGraph in PyROOT

**URL:** <https://root-forum.cern.ch/t/issue-converting-argument-for-tgraph-in-pyroot/39309>\
**Category:** ROOT\
**Tags:** python\
**Created:** [May 7, 2020, 7:05pm UTC](https://root-forum.cern.ch/t/issue-converting-argument-for-tgraph-in-pyroot/39309 "2020-05-07T19:05:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RJN](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@RJN](https://root-forum.cern.ch/u/RJN)\
**Post date:** [May 7, 2020, 7:05pm UTC](https://root-forum.cern.ch/t/issue-converting-argument-for-tgraph-in-pyroot/39309/1 "2020-05-07T19:05:41Z")

</div>

Hi, I am trying to write a macro using PyROOT that ultimately produces a canvas containing multiple TGraphs, however I am running into an issue whereby a get an error with converting arguments. More specifically

```auto
TypeError: void TGraph::SetPoint(int i, double x, double y) =>
    could not convert argument 2 (a float is required)

```

I understand from [this thread](https://root-forum.cern.ch/t/tgraphasymmerrors-can-not-convert-argument-in-with-pyroot/27710) that arguments for TGraphs need to be C-style arrays, specifically numpy arrays. Initially I had been using non-numpy arrays (which had worked for me in the past in other PyROOT applications), for example `array('d')`, and I hoped that simply changing these to numpy arrays with `np.array(x)` but that didn’t work and produced the error

```auto
TypeError: void TGraph::SetPoint(int i, double x, double y) =>
    could not convert argument 2 (only length-1 arrays can be converted to Python scalars)

```

I’ve attached a test example below that gives the first error.

```auto
import ROOT
from ROOT import TChain, TFile, TTree, TSelector, TGraph, TGraphErrors
from ROOT import gROOT
import numpy as np
from array import array

values_dict = {}
x = array('d') 
plots_dict = {}

for i in range (0,2):
    
    values_dict["%s_values"% (i)] = array('d')
    plots_dict["%s_plot"% (i)] = ROOT.TGraph()
    x.append(i)
    for j in range (0,3):
        values_dict["%s_values"% (i)].append(j)

c1 = ROOT.TCanvas("c1","",1200,600)
for n in range(0,2):
    ROOT.c1.cd(n+1)
    plots_dict["%s_plot"% (n)].SetPoint(2,x,values_dict["%s_values"% (n)])
    plots_dict["%s_plot"% (n)].Draw('AP')

```

Alternatively the penultimate line can be changed to

```auto
plots_dict["%s_plot" %(n)].SetPoint(2,np.array(x),np.array(values_dict["%s_values" %(n)]))

```

to give the second error.

Any advice on how to solve this would be much appreciated

---

<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:** [May 8, 2020, 7:45am UTC](https://root-forum.cern.ch/t/issue-converting-argument-for-tgraph-in-pyroot/39309/2 "2020-05-08T07:45:29Z")

</div>

> [@RJN](#):
>
> arguments for TGraphs need to be C-style arrays

It seems you are using [SetPoint](https://root.cern/doc/master/classTGraph.html#a0cd3af5cb1b7cdc249002e45a0b32318). SetPoint takes simple float values as input not arrays.

---

<div class="post-metadata">

**Author:** ![RJN](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@RJN](https://root-forum.cern.ch/u/RJN)\
**Post date:** [May 8, 2020, 4:25pm UTC](https://root-forum.cern.ch/t/issue-converting-argument-for-tgraph-in-pyroot/39309/3 "2020-05-08T16:25:02Z")

</div>

Thanks for the reply! Yes you’re quite right. I got far too wrapped up in worrying about the different objects ROOT would accept in Python vs C++ and lost sight of what I was actually doing.

The issue in this case was fixed by simply changing the penultimate line in the code above to

```auto
plots_dict["%s_plot" %(n)] = ROOT.TGraph(2,np.array(x),np.array(values_dict["%s_values" %(n)]))

```

Thanks for the help

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 22, 2020, 4:34pm UTC](https://root-forum.cern.ch/t/issue-converting-argument-for-tgraph-in-pyroot/39309/4 "2020-05-22T16:34:20Z")

</div>

Closed on May 22, 2020, 4:34pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
