# TGraph changes the value of point position

**URL:** <https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405>\
**Category:** ROOT\
**Tags:** bug, python, hist\
**Created:** [August 23, 2021, 2:53pm UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405 "2021-08-23T14:53:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomasz\_Bold](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tomasz_bold/32/20574_2.png) [@Tomasz\_Bold](https://root-forum.cern.ch/u/Tomasz_Bold)\
**Post date:** [August 23, 2021, 2:53pm UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/1 "2021-08-23T14:53:07Z")

</div>

Hi,  
I have this trvial python script to draw a simple TGraph (distilled from larger chunk of code):

```auto
points = [
(0, 2.836967833067155, 12399074.61),
(1, 6.954407211174568, 20542395.5),
(2, 11.970329210518978, 18499490.9),
(3, 19.424693860517632, 34068743.5),
(4, 29.43182028809318, 24889912.3),
(5, 39.414418675302066, 22688035.700000003),
(6, 49.38887514528897, 25505205.04),
(7, 59.363856464667286, 22036243.92),
(8, 69.3345592681046, 18366012.5),
(9, 79.3097616083224, 14811020.4),
(10, 89.28363506909325, 11568560.3),
(11, 103.50409212026426, 37625910.3),
(12, 119.21492822326776, 11485086.8),
(13, 133.24353243118534, 18563865.1),
(14, 153.08841322492458, 8265027.1),
(15, 172.95258012423116, 3801574.7),
(16, 195.74833705900488, 1737539.1),
(17, 227.38171871397918, 372180.6003 ),
(18, 269.56177047343357, 35339.70004 )]

import ROOT

gr = ROOT.TGraph(len(points))
for p, x, y in points:
  print(p,x,y)
  gr.SetPoint(p, x, y)
# OR
# gr.SetPointX(p, x)
# gr.SetPointY(p, y)

gr.Draw("AL*")

```

The graph is then drawn only when I click the canvas. But it comes out broken (see attached plot).  
Point 14 has x and y positions screwed up. Why point 14?

When I add `ROOT.gPad.Update()` at the end of the script the points come out as expected.  
Seems like the “update” event is messing up sth.  
 ![ROOT_TGraph_Draw_Bug](https://root-forum.cern.ch/uploads/default/original/3X/c/b/cb58393c9947e4fee9e4056d93768b2cd3036adc.png)

* * *

_ROOT Version:_ 6.24/02 , Python version: 3.9  
_Platform:_ MacOs BigSur  
_Compiler:_ Apple clang version 12.0.5 (clang-1205.0.22.11)

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 23, 2021, 3:32pm UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/2 "2021-08-23T15:32:54Z")

</div>

Hi @Tomasz_Bold ,  
I cannot reproduce the problem. I tried on my own laptop but also on LXPLUS:

```auto
$ ssh -XY eguiraud@lxplus.cern.ch
$ source /cvmfs/sft.cern.ch/lcg/views/LCG_100/x86_64-centos7-gcc10-opt/setup.sh
$ python -i repro.py # where repro.py is a copy-paste of the code you provided

```

displays

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/d/bd81f663b854523ab1b14dfa10a6cdae9101bc08.png)

What am I doing differently?

Cheers,  
Enrico

---

<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:** [August 23, 2021, 5:06pm UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/3 "2021-08-23T17:06:49Z")

</div>

Try:

> [@Updating using gPad-\>Modified() and gPad-\>Update()](https://root-forum.cern.ch/t/updating-using-gpad-modified-and-gpad-update/27821/2):
>
> Maybe you are facing [a bug with the Cocoa version on Mac which was discovered some time ago](https://root-forum.cern.ch/t/canvas-will-not-show/27582/10). After each “Update”, try to add gSystem-\>ProcessEvents(); (executed once or two times). BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

---

<div class="post-metadata">

**Author:** ![Tomasz\_Bold](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tomasz_bold/32/20574_2.png) [@Tomasz\_Bold](https://root-forum.cern.ch/u/Tomasz_Bold)\
**Post date:** [August 23, 2021, 7:41pm UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/4 "2021-08-23T19:41:02Z")

</div>

Thanks for these hints. Adding `ROOT.gSystem.ProcessEvents()` did not help. Though, a drawing sequence when the canvas is made first seems to do the job for now. Quite flaky though.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 24, 2021, 7:40am UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/5 "2021-08-24T07:40:28Z")

</div>

Ah so it could be a Cocoa (i.e. MacOS-only) bug. @couet can you reproduce the problem on mac maybe? Or do you have some more insight into the Cocoa bug that Wile E. mentioned?

---

<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:** [August 24, 2021, 8:02am UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/6 "2021-08-24T08:02:03Z")

</div>

On Mac, if I do:

```auto
% python tomasz.py

```

where `tomasz.py` is:

```auto
points = [
(0, 2.836967833067155, 12399074.61),
(1, 6.954407211174568, 20542395.5),
(2, 11.970329210518978, 18499490.9),
(3, 19.424693860517632, 34068743.5),
(4, 29.43182028809318, 24889912.3),
(5, 39.414418675302066, 22688035.700000003),
(6, 49.38887514528897, 25505205.04),
(7, 59.363856464667286, 22036243.92),
(8, 69.3345592681046, 18366012.5),
(9, 79.3097616083224, 14811020.4),
(10, 89.28363506909325, 11568560.3),
(11, 103.50409212026426, 37625910.3),
(12, 119.21492822326776, 11485086.8),
(13, 133.24353243118534, 18563865.1),
(14, 153.08841322492458, 8265027.1),
(15, 172.95258012423116, 3801574.7),
(16, 195.74833705900488, 1737539.1),
(17, 227.38171871397918, 372180.6003 ),
(18, 269.56177047343357, 35339.70004 )]

import ROOT

c1 = ROOT.TCanvas()

gr = ROOT.TGraph(len(points))
for p, x, y in points:
  print(p,x,y)
  gr.SetPoint(p, x, y)

gr.Draw("AL*")
c1.SaveAs("plot.png")

```

I get the correct plot:

 ![plot](https://root-forum.cern.ch/uploads/default/original/3X/2/a/2a40f78d642679cbada094b8619c97d313f025cb.png)

---

<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:** [September 7, 2021, 8:02am UTC](https://root-forum.cern.ch/t/tgraph-changes-the-value-of-point-position/46405/7 "2021-09-07T08:02:11Z")

</div>

Closed on September 7, 2021, 8:02am UTC

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