# Problems by generating a graph

**URL:** <https://root-forum.cern.ch/t/problems-by-generating-a-graph/42617>\
**Category:** Newbie\
**Created:** [December 6, 2020, 6:18pm UTC](https://root-forum.cern.ch/t/problems-by-generating-a-graph/42617 "2020-12-06T18:18:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NicolasBiscione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@NicolasBiscione](https://root-forum.cern.ch/u/NicolasBiscione)\
**Post date:** [December 6, 2020, 6:18pm UTC](https://root-forum.cern.ch/t/problems-by-generating-a-graph/42617/1 "2020-12-06T18:18:14Z")

</div>

Hi everyone,

I’m working with ROOT 6.22/02 for a university exam using a virtual version of ubuntu on Repl. I’m compiling with g++.

My program should show with TGraph the trend of the intensity of the electric field generated on the axis of two charges, starting from 100 times a given distance to 1000 times that same distance.

It mostly works, but I noticed that some of the points on the graph are set to wrong positions, I really don’t understand where the problem could be.

This is the code:

```auto
  TApplication myapp("app",0,0);

  TGraph * field = new TGraph;

	for ( double i = 1; i <=10; i+=0.2) {

		double yy = 100.*i*delta;	
		
		CampoVettoriale CV = e.CampoElettrico(delta/2,yy,0) + p.CampoElettrico(delta/2,yy,0);

		field->SetPoint(i*5-4,i*5-4,CV.Modulo());
}	

	field->Draw("ALP");
	field->SetMarkerColorAlpha(6,0.30);
  field->SetMarkerStyle(48);
  field->SetMarkerSize(1);

	myapp.Run();

```

It’s my firts post on here so I hope that it’s appropriate.  
Thanks for help.

Nicolas

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [December 6, 2020, 7:46pm UTC](https://root-forum.cern.ch/t/problems-by-generating-a-graph/42617/2 "2020-12-06T19:46:57Z")

</div>

Hi Nicolas,

There are three points.  
First, when you create `TGraph`, it is better to specify number of points:

```auto
  TGraph * field = new TGraph(46);

```

Second, numbering of points starts from 0.  
And third, when you set point via index, index should be integer value:

```auto
field->SetPoint(TMath::Nint(i*5-5),i*5-4,CV.Modulo());

```

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![NicolasBiscione](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/a88e57/32.png) [@NicolasBiscione](https://root-forum.cern.ch/u/NicolasBiscione)\
**Post date:** [December 6, 2020, 9:55pm UTC](https://root-forum.cern.ch/t/problems-by-generating-a-graph/42617/3 "2020-12-06T21:55:22Z")

</div>

Hi Sergey, thanks for helping, this solved my problems.

Nicolas
