# Using a 2D array to plot with TGraph

**URL:** <https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511>\
**Category:** Newbie\
**Created:** [March 21, 2020, 4:16am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511 "2020-03-21T04:16:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_man](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@T\_man](https://root-forum.cern.ch/u/T_man)\
**Post date:** [March 21, 2020, 4:16am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/1 "2020-03-21T04:16:05Z")

</div>

I am trying to plot a TGraph (Root cern) using data stored in a 2D array. I have defined my 2D array like this

```auto
double**meansig = new double*[NPIXAX];
for(int i = 0; i < NPIXAX; i++){
meansig[i] = new double[NPIXAY];}

```

There are 2 more arrays defined the same way. I want to plot the graph using TGraph and I tried to do it like this

```auto
TGraph* g = new TGraph(pixn,pixid,meansig);

```

but that didn’t work so I tried this

```auto
TGraph* g = new TGraph(pixn,*pixid,*meansig);

```

and this

```auto
TGraph* g = new TGraph(pixn, **pixid,** meansig);

```

but they didn’t work either, I would appreciate any advice

Thanks

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 21, 2020, 7:25am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/2 "2020-03-21T07:25:49Z")

</div>

[TGraph](https://root.cern/doc/master/classTGraph.html) needs simple one dimensional arrays, so try:  
`double *meansig = new double[((NPIXAX) * (NPIXAY))];`

---

<div class="post-metadata">

**Author:** ![T\_man](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@T\_man](https://root-forum.cern.ch/u/T_man)\
**Post date:** [March 21, 2020, 7:33am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/3 "2020-03-21T07:33:55Z")

</div>

I need the 2D array as the data is extracted from a 2D histogram from a root file, so unfortunately having a 2D array is important

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 21, 2020, 7:52am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/4 "2020-03-21T07:52:43Z")

</div>

What is the “x” and “y” of the points?

Maybe you want a [TGraph2D](https://root.cern/doc/master/classTGraph2D.html) for 2D data.

---

<div class="post-metadata">

**Author:** ![T\_man](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@T\_man](https://root-forum.cern.ch/u/T_man)\
**Post date:** [March 21, 2020, 8:28am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/5 "2020-03-21T08:28:55Z")

</div>

in this case x is the ID of the pixel and y is the mean of the signal

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 21, 2020, 8:33am UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/6 "2020-03-21T08:33:43Z")

</div>

```cpp
TGraph *g = new TGraph(((NPIXAX) * (NPIXAY)));
for (int x = 0; x < (NPIXAX); x++)
  for (int y = 0; y < (NPIXAY); y++)
    g->SetPoint(((NPIXAY) * x + y), pixid[x][y], meansig[x][y]);

```

---

<div class="post-metadata">

**Author:** ![T\_man](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@T\_man](https://root-forum.cern.ch/u/T_man)\
**Post date:** [March 21, 2020, 2:12pm UTC](https://root-forum.cern.ch/t/using-a-2d-array-to-plot-with-tgraph/38511/7 "2020-03-21T14:12:06Z")

</div>

Thanks, that did work
