Equal pad size in TCanvas with one common axis

void CanvasPartition(TCanvas *C,const Int_t Nx = 2,const Int_t Ny = 2,
                     Float_t lMargin = 0.15, Float_t rMargin = 0.05,
                     Float_t bMargin = 0.15, Float_t tMargin = 0.05);

void canvas2()
{
   auto C = new TCanvas("C","canvas",1024,640);
   C->SetFillStyle(4000);

   // Number of PADS
   const Int_t Nx = 3;
   const Int_t Ny = 2;

   // Margins
   Float_t lMargin = 0.12;
   Float_t rMargin = 0.05;
   Float_t bMargin = 0.15;
   Float_t tMargin = 0.05;

   // Canvas setup
   CanvasPartition(C,Nx,Ny,lMargin,rMargin,bMargin,tMargin);
   TPad *pad[Nx][Ny];

   for (Int_t i=0;i<Nx;i++) {
      for (Int_t j=0;j<Ny;j++) {
         C->cd(0);
         // Get the pads previously created.
         char pname[16];
         sprintf(pname,"pad_%i_%i",i,j);
         pad[i][j] = (TPad*) gROOT->FindObject(pname);
         pad[i][j]->Draw();
         pad[i][j]->SetFillStyle(4000);
         pad[i][j]->SetFrameFillStyle(4000);
         pad[i][j]->cd();
      }
   }

   // Dummy histogram.
   gStyle->SetOptStat(0);
   auto h = new TH1F("histo","",100,-5.0,5.0);
   h->FillRandom("gaus",10000);
   h->GetXaxis()->SetTitle("x axis");
   h->GetYaxis()->SetTitle("y axis");

   pad[0][0]->cd(); h->Draw("");
   pad[1][0]->cd(); h->Draw("");
   pad[2][0]->cd(); h->Draw("");

   pad[0][1]->cd(); h->Draw("");
   pad[1][1]->cd(); h->Draw("");
   pad[2][1]->cd(); h->Draw("");

}



void CanvasPartition(TCanvas *C,const Int_t Nx,const Int_t Ny,
                     Float_t lMargin, Float_t rMargin,
                     Float_t bMargin, Float_t tMargin)
{
   if (!C) return;

   // Setup Pad layout:
   Float_t vSpacing = 0.0;
   Float_t vStep  = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;

   Float_t hSpacing = 0.0;
   Float_t hStep  = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx;

   Float_t vposd,vposu,vmard,vmaru,vfactor;
   Float_t hposl,hposr,hmarl,hmarr,hfactor;

   for (Int_t i=0;i<Nx;i++) {

      if (i==0) {
         hposl = 0.0;
         hposr = lMargin + hStep;
         hfactor = hposr-hposl;
         hmarl = lMargin / hfactor;
         hmarr = 0.0;
      } else if (i == Nx-1) {
         hposl = hposr + hSpacing;
         hposr = hposl + hStep + rMargin;
         hfactor = hposr-hposl;
         hmarl = 0.0;
         hmarr = rMargin / (hposr-hposl);
      } else {
         hposl = hposr + hSpacing;
         hposr = hposl + hStep;
         hfactor = hposr-hposl;
         hmarl = 0.0;
         hmarr = 0.0;
      }

      for (Int_t j=0;j<Ny;j++) {

         if (j==0) {
            vposd = 0.0;
            vposu = bMargin + vStep;
            vfactor = vposu-vposd;
            vmard = bMargin / vfactor;
            vmaru = 0.0;
         } else if (j == Ny-1) {
            vposd = vposu + vSpacing;
            vposu = vposd + vStep + tMargin;
            vfactor = vposu-vposd;
            vmard = 0.0;
            vmaru = tMargin / (vposu-vposd);
         } else {
            vposd = vposu + vSpacing;
            vposu = vposd + vStep;
            vfactor = vposu-vposd;
            vmard = 0.0;
            vmaru = 0.0;
         }

         C->cd(0);

         char name[16];
         sprintf(name,"pad_%i_%i",i,j);
         TPad *pad = (TPad*) gROOT->FindObject(name);
         if (pad) delete pad;
         pad = new TPad(name,"",hposl,vposd,hposr,vposu);
         pad->SetLeftMargin(hmarl);
         pad->SetRightMargin(hmarr);
         pad->SetBottomMargin(vmard);
         pad->SetTopMargin(vmaru);

         pad->SetFrameBorderMode(0);
         pad->SetBorderMode(0);
         pad->SetBorderSize(0);

         pad->Draw();
      }
   }
}
1 Like

multipad.C (10.9 KB)

I am trying as in your code above, but it is drawing an empty canvas, please see my code.

With your macro I get this:

I see that the histogram h33 is empty… that why you get an empty pad

multipad.C (10.8 KB)

Finally, I have the plot as I wanted. Now, I am trying to set a single x and y axis label in the center of the entire canvas… how can I do that ?
Example- example

Use TText to draw the title where you want.

I have to write it outside the pads, on the canvas area in the center along Y axis, how will the position of the text be specified ?

   pad[0][0]->cd(); h->Draw("");
   pad[1][0]->cd(); h->Draw("");

   TLatex *tex = new TLatex();
   tex->SetTextAlign(22);
   tex->SetTextSize(0.1415929);
   tex->DrawLatexNDC(0.5,0.1,"Global Title");

   pad[2][0]->cd(); h->Draw("");

   pad[0][1]->cd(); h->Draw("");
   pad[1][1]->cd(); h->Draw("");
   pad[2][1]->cd(); h->Draw("");

1 Like

Hi, there is one problem with this text, it is rotating only in the first half of the canvas and not as I want. I am attaching a screenshot of the y-axis that I want. I have tried it with various orientations but it is not coming out as I want. screenshot- example

See my example. The title is located on pad[1][0] centered.

yes you are right, it is giving me along X axis, I want it in a similar way centered along Y axis…

You do the same with an other TLatex with an angle of 90 degrees (SetAngle)

unnamed1.pdf (22.0 KB)

Its getting half hidden as I set its angle to 90 degrees. Please see the attached pdf.

canvas2.C (3.9 KB)

1 Like

Thank you so much, finally it’s done!