Centering xLabel and yLabel

I am writing a Root macro that plots multiple TGraph-s on a single TCanvas. A simple test:

TCanvas *canvas=new TCanvas("Plot 1","Plot 1");
float x[] = {0.5,0.5,1.5,1.5,2.5,2.5,3.5,3.5,4.5,4.5,5.5,5.5,6.5,6.5,7.5,7.5,8.5,8.5,9.5,9.5,};
float y[] = {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,};
canvas->DrawFrame(0.5,0,9.5,9,"Chart Title;X Label;Y Label");
TGraph *graph  = new TGraph(20,x,y);
graph->Draw("L");
graph->GetXaxis()->CenterTitle();
graph->GetYaxis()->CenterTitle();
canvas->Update();

However the xLabel and yLabel are drawn at the top and right, not centered on their axes. This looks ugly (why is that the default???).

How can I center the xLabel and yLabel of the axes?
I have been unable to find any analog to graph->GetXaxis() for TCanvas or TPad, and centering that does nothing.

Tom Roberts

@couet could you please help with this one?

Because this is the most common way to display the axis tittle: at the end of the axis. Of course this can be changed. You found the right method but you did not apply it on the right object. Here is the working version of you macro:

{
   TCanvas *canvas=new TCanvas("Plot1","Plot1");
   float x[] = {0.5,0.5,1.5,1.5,2.5,2.5,3.5,3.5,4.5,4.5,5.5,5.5,6.5,6.5,7.5,7.5,8.5,8.5,9.5,9.5,};
   float y[] = {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,};
   TH1F *f = canvas->DrawFrame(0.5,0,9.5,9,"Chart Title;X Label;Y Label");
   TGraph *graph  = new TGraph(20,x,y);
   graph->Draw("L");
   f->GetXaxis()->CenterTitle();
   f->GetYaxis()->CenterTitle();
}

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