SetTitle TGraph

Hi everybody :slight_smile:

I have need your help, I have already read the ROOT guide, but I’m a beginner with root.
I have set the Graph’s title:

gr->SetTitle(“title”) ;

Then I would like set the position of the Graph’s title,nut not only top, center, left,ecc but for all coordinate in the canvas, how can i do this?

Another question is, how I can insert the tex when I have a canvas divide in 3?
Because when the canvas is only 1, I have not problem with these comand:

{
TText *t = new TText(.5,.5,“Hello World !”);
t->SetTextAlign(22);
t->SetTextColor(kRed+2);
t->SetTextFont(43);
t->SetTextSize(40);
t->SetTextAngle(45);
t->Draw();
}

But when the canvas is divided I don’t see the text.

Thank you very much in advance :wink:

  1. you cannot set the position of the title relatively to the pad coordinates. If you want to do that you need to use TText of TLatex.

  2. Before drawing in a sub-pad you have to do cd() on it. If “c” is your canvas you need to do c->cd(3) before drawing the text in pad #3.

Thank you very much for the answer!!! :wink:

before drawing I move in c->cd(3) where i want the tex, I draw the Graph and finally insert the tex, but i don’t see it. I have tried to insert the txt befor to draw the graph but the result it is same. how can I do?

thanks a lot :wink:

Post here a small macro reproducing your problem.

It’ only one piece of the macro, the one that contains the text:

{
TCanvas *canvas = new TCanvas("canvas","canvas");		
canvas->Divide(3,1);						
canvas->cd(3);

TText *t = new TText(.5,.9,"CHAMBER");
t->SetTextAlign(22);
t->SetTextFont(43);
t->SetTextSize(20);
t->Draw();

gPad->SetGrid();

gPad->SetBottomMargin(0.07); gPad->SetTopMargin(0.18);			

TGraph *c1 = new TGraph ("1tw"); 
c1->SetLineStyle(1); c1->SetLineColor(kBlue);  c1->SetLineWidth(2); c1->SetMarkerStyle(20); c1->SetMarkerSize(0.9); c1->SetMarkerColor(kBlue);
TGraph *c2 = new TGraph ("2tw"); 
c2->SetLineStyle(1); c2->SetLineColor(kRed);  c2->SetLineWidth(2); c2->SetMarkerStyle(21); c2->SetMarkerSize(0.9); c2->SetMarkerColor(kRed);
TGraph *c3 = new TGraph ("3tw");
c3->SetLineStyle(1); c3->SetLineColor(kGreen);  c3->SetLineWidth(2); c3->SetMarkerStyle(22); c3->SetMarkerSize(0.9); c3->SetMarkerColor(kGreen); 

TMultiGraph * c = new TMultiGraph();

c->Add(c1);
c->Add(c2);
c->Add(c3);

c->Draw("apl"); 

c->SetTitle("TW");
c->GetXaxis()->SetLimits(8600,10000.0); 
c->SetMinimum(0.); c->SetMaximum(2.);
c->GetXaxis()->SetLabelSize(0.03); c->GetYaxis()->SetLabelSize(0.03);		
c->GetXaxis()->SetLabelOffset(0.0); c->GetYaxis()->SetLabelOffset(.015);
c->GetXaxis()->SetTickLength(0.015); c->GetYaxis()->SetTickLength(0.02);
c->GetXaxis()->SetNdivisions(20,5,0); c->GetYaxis()->SetNdivisions(20,5,0);


canvas->SaveAs("plot1.png");
}

Thank you very much :wink:

Try this:

{
   TCanvas *canvas = new TCanvas("canvas","canvas");
   canvas->Divide(3,1);

   canvas->cd(1);
   TGraph *g[3];
   Double_t x[10] = {0,1,2,3,4,5,6,7,8,9};
   Double_t y[10] = {1,2,3,4,5,5,4,3,2,1};
   TMultiGraph *mg = new TMultiGraph();

   for (int i=0; i<3; i++) {
      g[i] = new TGraph(10, x, y);
      g[i]->SetMarkerStyle(20);
      g[i]->SetMarkerColor(i+2);
      for (int j=0; j<10; j++) y[j] = y[j]-1;
      mg->Add(g[i]);
   }
   mg->Draw("APL");
   mg->SetTitle("Global title; X Axis; Y Axis");

   canvas->cd(3);
   TText *t3 = new TText(.5,.9,"CHAMBER 3");
   t3->SetTextAlign(22);
   t3->SetTextFont(43);
   t3->SetTextSize(20);
   t3->Draw();
}

I have to insert the text only in the pad3, the problem it si that insert the text befor drawing or after drawing, the text don’t see it.

Is that macro working for you ? … if yes do the same …

where is the difference? I have also put the text befor the draw and don’t work, instead in your macro yes, why?

thank you

Where do you want the text ? in pad 1 ? 2 ? 3 ?

I took the example of you 1st macro … it was in pad 3…

Ok here is the example with the text on Pad 1:

{
   TCanvas *canvas = new TCanvas("canvas","canvas");
   canvas->Divide(3,1);

   canvas->cd(1);
   TGraph *g[3];
   Double_t x[10] = {0,1,2,3,4,5,6,7,8,9};
   Double_t y[10] = {1,2,3,4,5,5,4,3,2,1};
   TMultiGraph *mg = new TMultiGraph();

   for (int i=0; i<3; i++) {
      g[i] = new TGraph(10, x, y);
      g[i]->SetMarkerStyle(20);
      g[i]->SetMarkerColor(i+2);
      for (int j=0; j<10; j++) y[j] = y[j]-1;
      mg->Add(g[i]);
   }
   mg->Draw("APL");
   mg->SetTitle("Global title; X Axis; Y Axis");

   canvas->cd(1);
   TText *t1 = new TText(.5,.9,"CHAMBER 1");
   t1->SetNDC(true);
   t1->SetTextAlign(22);
   t1->SetTextFont(43);
   t1->SetTextSize(20);
   t1->Draw();
}

The problem it isn’t where i would like the text, because it is easy change pad, it’s enough change c->cd()

I have tried to insert my data graph in your macro, in place of the cycle for, but don’t work. why?

thank you :wink:

I am sorry but I do not understand what you mean.
Send me a running macro (your first example did not work), showing what is wrong …
That’s very unclear for me right now…

Ok , maybe it was the legend that created a problem because now work!!

Thank you very much for the help :wink: