TGaxis title alignment

Hi there,

I’m trying to scale my histogram axis by hand, i.e. painting a TGaxis with a different range on top of the TAxis drawn by default. The TGaxis plots fine except for one thing - the title won’t align with the top of the frame. I tried calling TAxis::CenterTitle(kFALSE) and as a cross check also with a kTRUE argument and I can see that it changes the fBits field, but it doesn’t move the axis title. Does anybody know how I can get the title properly aligned with the top of the frame?

Thanks,
Bram

I’m using this weekend’s CVS version compiled into my own code, code snippet below:

   if(hdata->GetMaximum() > 1000) {
  yaxis = new TGaxis(0, 0, 0, hdata->GetMaximum(), 0, hdata->GetMaximum()/1000., 505, "");
  yaxis->ImportAxisAttributes(hdata->GetYaxis());
  hdata->GetYaxis()->SetTitle("");
  hdata->GetYaxis()->SetTitleColor(WHITE);
  hdata->GetYaxis()->SetTitleSize(0.01);
  hdata->GetYaxis()->SetLabelSize(0.01);
  hdata->GetYaxis()->SetLabelColor(WHITE);
  yaxis->SetLabelColor(BLACK);
  yaxis->SetLabelSize(0.07);
  yaxis->SetTitleSize(0.07);
  yaxis->SetTitle(Form("#times1000 entries/%g GeVc^{-1}",(Double_t)(hdata->GetXaxis()->GetXmax()/hdata->GetNbinsX())));
  yaxis->SetTextColor(BLACK);
  yaxis->CenterTitle(kFALSE);
  yaxis->DrawAxis(0, 0, 0, hdata->GetMaximum(), 0, hdata->GetMaximum()/1000., 505, "");
   }

Can you send a small example I can run ?

Thanks, Olivier

Here goes - the executable should not depend on anything but ROOT, histogram is in the attached file test0.root.

Bram
test0.root (3.75 KB)
test.cxx (1.58 KB)

The Yaxis position you choose is not correct. Pad maximum is not the histogram maximum (there is a margin). Change axis color to red and you will see that. Here is a working example:

void test() {
   TCanvas* c1 = new TCanvas("c1", "Test axis");

   gStyle->SetPadLeftMargin(0.14);
   gStyle->SetPadBottomMargin(0.15);
   gStyle->SetLabelSize(0.07, "xyz");
   gStyle->SetTitleSize(0.07, "xyz");
   gStyle->SetTitleH(0.08); // width is adjusted according to string length
   gStyle->SetTitleX(0.14); // flush with the left side of the frame, I hope
   gStyle->SetTitleStyle(0);

   TFile* ftest = new TFile("test0.root", "read");
   TH1F* hdata = (TH1F*) ftest->Get("hPtRelPhi_5");
   hdata->DrawCopy("ep");
   c1->Update();

   TGaxis* yaxis;
   if(hdata->GetMaximum() > 1000) {
      yaxis = new TGaxis(0, 0, 0, gPad->GetUymax(), 0, gPad->GetUymax()/1000., 510, "");     
      yaxis->ImportAxisAttributes(hdata->GetYaxis());
      hdata->GetYaxis()->SetTitleOffset(999.);
      hdata->GetYaxis()->SetLabelOffset(999.);
      hdata->DrawCopy("ep");
      yaxis->SetLineColor(2); 
      yaxis->SetLabelSize(0.03);
      yaxis->SetTitleSize(0.03);
      yaxis->SetTitle("Y Title align on top (use GetUymax)");
      yaxis->Draw();
   }
}

Thanks!

I had tried the gPad->Uymax() method before (it’s in the examples) but I never called TPad::Update() , so gPad->Uymax() returned 1.

Yes, gPad->Uymax returns the Y maximum value of the current pad. But tobe right, the pad should have been created. That’s what Update() is doing.