TGraph Point Label

Hi Everybody,
I wanted to know if it is possible in TGraph to show a label near a point with the indication of its quote. If anybody can help me I thank him a lot!
Bye

Pietro

Assuming a TGraph* gr object, you can add an annotation object ,eg TText, TLatex, TPaveLabel, etc corresponding to a point in the graph with something like:

TLatex *latex = new TLatex(gr->GetX()[5], gr->GetY()[5],"my annotation"); gr->GetListOfFunctions()->Add(latex); gr->Draw("alp");
It writes an annotation (left adjusted) at the 6-th point of the graph.
You can set the normal graphics attributes for the annotation, eg

latex->SetTextSize(0.07); latex->SetTextColor(kRed);
When drawing the graph, the annotation (or annotations) will be automatically drawn.
If you write the graph to a file, the annotation(s) will also be written.

Rene

Thank you very much!

Pietro

the line

graf->GetListOfFunctions()->Add(latex);

give me an error

Error: illegal pointer to class object GetListOfFunctions() 0x0 14 graph.C:145:
*** Interpreter error recovered ***

How can I manage to solve the problem?

Thank you

Pietro

Could you send the shortest possible running script reproducing the problem? In case you run with an old version, upgrade your version first.

Rene

g[i] is an int array
c[i] is a TCanvas* array
pp is a TCanvas*
x and y are double arrays

[code]for(int i=0;i<6;i++){
if(g[i]<0)continue;
pp=c[i];
pp->cd();

TGraph *graf = new TGraph();
int aus=0;
for(int j=g[i];j<N;j++){
	double div, actq, usl;
	int aus2=g[i];
	int att=0;
	if(aus==0){while(1>0){div=y[aus2];
		   	      if(fabs(div)>.00000001)break;
			      aus2--;
			      att++;
			      }
		   }
	if(att!=0 && j==g[i]){actq=(y[aus2]/div);}
	else{actq=(y[j]/div);}
	if(fabs(actq)<.0000000001)graf->GetPoint(aus-1,usl,actq);
	graf->SetPoint(aus,x[j],actq);
	aus++;
	}
TLatex *latex = new TLatex(graf->GetX()[4],graf->GetY()[4],"pippo");
graf->GetListOfFunctions()->Add(latex);[/code]

I’m using ROOT Version 5.12/00 July 06.
Thank You

Bye
Pietro[/code]

Replace the line

TGraph *graf = new TGraph(); by

TGraph *graf = new TGraph(0); or better

TGraph *graf = new TGraph(N);
Rene

Thanks

Pietro

This is valid for a single point. But if I want to do it for multiple points in a loop, then how to do it?

void graphtext2() {
   TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500);
   c->SetGrid();
   const Int_t n = 10;
   TGraph *gr = new TGraph(n);
   gr->SetTitle("A Simple Graph Example with Text");
   gr->SetMarkerStyle(20);
   TExec *ex = new TExec("ex","DrawText();");

   gr->GetListOfFunctions()->Add(ex);

   Double_t x, y;
   for (Int_t i=0;i<n;i++) {
      x = i*0.1;
      y = 10*sin(x+0.2);
      gr->SetPoint(i,x,y);

   }

   gr->Draw("ALP");
}


void DrawText()
{
   Int_t i,n;
   Double_t x,y;
   TLatex *l;

   TGraph *g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
   n = g->GetN();
   for (i=1; i<n; i++) {
      g->GetPoint(i,x,y);
      l = new TLatex(x,y+0.2,Form("%4.2f",y));
      l->SetTextSize(0.025);
      l->SetTextFont(42);
      l->SetTextAlign(21);
      l->Paint();
   }
}

1 Like

Please don’t reply to old topics (17 years old!)

1 Like