Centering pad on canvas

Hey there,

I am creating canvases divided into 6 pads (2 columns * 3 rows), but I only use 5 of them. Is there a way to vertically center the last one?

Cheers,
Adam Hunyadi

You should use the first four and create the fifth one centred (via the normal pad constructor).

You can also do this if you want the last pad stretched:

{
   gStyle->SetPadBorderSize(1);

   TCanvas *c = new TCanvas("c", "C");
    
   //First divide into rows.
   c->Divide(1, 3); 
   //Then divide first two rows into columns.
   c->cd(1)->Divide(2, 1);
   c->cd(2)->Divide(2, 1);

   //Push each pad into a vector.
   std::vector<TVirtualPad*> pads;

   pads.push_back(c->cd(1)->cd(1));
   pads.push_back(c->cd(1)->cd(2));
   pads.push_back(c->cd(2)->cd(1));
   pads.push_back(c->cd(2)->cd(2));
   pads.push_back(c->cd(3));

   // Color the pads so we can inspect them.
   for (int padNum=0; padNum < pads.size(); ++padNum) {
      TVirtualPad *& pad = pads.at(padNum); 
      pad->SetFillStyle(1001);
      pad->SetFillColor(padNum + 1);
      pad->Modified();
   }  
      
   c->Update()
}  

2 Likes

How do I create a vertically centered TPad then? :slight_smile:

The yellow pad @ksmith posted is vertically centred … May be you would need to define/described what you are looking for exactly. Do you mean a pad with the same size as the others but centred along X in the middle of the page and along Y placed in the empty space remaining when you have drawn the first 4 pads ?

Yes, that is exactly what I am looking for.

{
   TCanvas *c = new TCanvas("c", "c",600,800);
   c->Divide(2,3);
   TPad *pad;

   for (int padNum=1; padNum <= 4; ++padNum) {
      pad = (TPad*)c->cd(padNum);
      pad->SetFillStyle(1001);
      pad->SetFillColor(padNum + 1);
      pad->Modified();
   }
   pad = (TPad*)c->cd(5);
   Double_t yl = pad->GetAbsYlowNDC();
   Double_t w = pad->GetAbsWNDC();
   Double_t h = pad->GetAbsHNDC();
   Double_t xl = (1.-w)/2.;

   c->cd(0);
   TPad *p5 = new TPad("p5","p5",xl,yl,xl+w,yl+h,6);
   p5->Draw();
}

3 Likes

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