From $ROOTSYS/tutorials/graphics/canvas2.C

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()
{
//Example of canvas partitioning
// Sometimes the Divide() method is not appropriate to divide a Canvas.
// Because of the left and right margins, all the pads do not have the
// same width and height. CanvasPartition does that properly. This
// example also ensure that the axis labels and titles have the same
// sizes and that the tick marks length is uniform.
//Author:

   gStyle->SetOptStat(0);

   TCanvas *C = (TCanvas*) gROOT->FindObject("C");
   if (C) delete C;
   C = new TCanvas("C","canvas",1024,640);
   C->SetFillStyle(4000);

   // Number of PADS
   const Int_t Nx = 5;
   const Int_t Ny = 5;

   // 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);

   // Dummy histogram.
   TH1F *h = (TH1F*) gROOT->FindObject("histo");
   if (h) delete h;
   h = new TH1F("histo","",100,-5.0,5.0);
   h->FillRandom("gaus",10000);
   h->GetXaxis()->SetTitle("x axis");
   h->GetYaxis()->SetTitle("y axis");

   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 previosly 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();

         // Size factors
         Float_t xFactor = pad[0][0]->GetAbsWNDC()/pad[i][j]->GetAbsWNDC();
         Float_t yFactor = pad[0][0]->GetAbsHNDC()/pad[i][j]->GetAbsHNDC();

         char hname[16];
         sprintf(hname,"h_%i_%i",i,j);
         TH1F *hFrame = (TH1F*) h->Clone(hname);
         hFrame->Reset();
         hFrame->Draw();

         // y axis range
         hFrame->GetYaxis()->SetRangeUser(0.0001,1.2*h->GetMaximum());

         // Format for y axis
         hFrame->GetYaxis()->SetLabelFont(43);
         hFrame->GetYaxis()->SetLabelSize(16);
         hFrame->GetYaxis()->SetLabelOffset(0.02);
         hFrame->GetYaxis()->SetTitleFont(43);
         hFrame->GetYaxis()->SetTitleSize(16);
         hFrame->GetYaxis()->SetTitleOffset(5);

         hFrame->GetYaxis()->CenterTitle();
         hFrame->GetYaxis()->SetNdivisions(505);

         // TICKS Y Axis
         hFrame->GetYaxis()->SetTickLength(xFactor*0.04/yFactor);

         // Format for x axis
         hFrame->GetXaxis()->SetLabelFont(43);
         hFrame->GetXaxis()->SetLabelSize(16);
         hFrame->GetXaxis()->SetLabelOffset(0.02);
         hFrame->GetXaxis()->SetTitleFont(43);
         hFrame->GetXaxis()->SetTitleSize(16);
         hFrame->GetXaxis()->SetTitleOffset(5);
         hFrame->GetXaxis()->CenterTitle();
         hFrame->GetXaxis()->SetNdivisions(505);

         // TICKS X Axis
         hFrame->GetXaxis()->SetTickLength(yFactor*0.06/xFactor);

         h->Draw("same");
      }
   }
   C->cd();
}



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();
      }
   }
}
 canvas2.C:1
 canvas2.C:2
 canvas2.C:3
 canvas2.C:4
 canvas2.C:5
 canvas2.C:6
 canvas2.C:7
 canvas2.C:8
 canvas2.C:9
 canvas2.C:10
 canvas2.C:11
 canvas2.C:12
 canvas2.C:13
 canvas2.C:14
 canvas2.C:15
 canvas2.C:16
 canvas2.C:17
 canvas2.C:18
 canvas2.C:19
 canvas2.C:20
 canvas2.C:21
 canvas2.C:22
 canvas2.C:23
 canvas2.C:24
 canvas2.C:25
 canvas2.C:26
 canvas2.C:27
 canvas2.C:28
 canvas2.C:29
 canvas2.C:30
 canvas2.C:31
 canvas2.C:32
 canvas2.C:33
 canvas2.C:34
 canvas2.C:35
 canvas2.C:36
 canvas2.C:37
 canvas2.C:38
 canvas2.C:39
 canvas2.C:40
 canvas2.C:41
 canvas2.C:42
 canvas2.C:43
 canvas2.C:44
 canvas2.C:45
 canvas2.C:46
 canvas2.C:47
 canvas2.C:48
 canvas2.C:49
 canvas2.C:50
 canvas2.C:51
 canvas2.C:52
 canvas2.C:53
 canvas2.C:54
 canvas2.C:55
 canvas2.C:56
 canvas2.C:57
 canvas2.C:58
 canvas2.C:59
 canvas2.C:60
 canvas2.C:61
 canvas2.C:62
 canvas2.C:63
 canvas2.C:64
 canvas2.C:65
 canvas2.C:66
 canvas2.C:67
 canvas2.C:68
 canvas2.C:69
 canvas2.C:70
 canvas2.C:71
 canvas2.C:72
 canvas2.C:73
 canvas2.C:74
 canvas2.C:75
 canvas2.C:76
 canvas2.C:77
 canvas2.C:78
 canvas2.C:79
 canvas2.C:80
 canvas2.C:81
 canvas2.C:82
 canvas2.C:83
 canvas2.C:84
 canvas2.C:85
 canvas2.C:86
 canvas2.C:87
 canvas2.C:88
 canvas2.C:89
 canvas2.C:90
 canvas2.C:91
 canvas2.C:92
 canvas2.C:93
 canvas2.C:94
 canvas2.C:95
 canvas2.C:96
 canvas2.C:97
 canvas2.C:98
 canvas2.C:99
 canvas2.C:100
 canvas2.C:101
 canvas2.C:102
 canvas2.C:103
 canvas2.C:104
 canvas2.C:105
 canvas2.C:106
 canvas2.C:107
 canvas2.C:108
 canvas2.C:109
 canvas2.C:110
 canvas2.C:111
 canvas2.C:112
 canvas2.C:113
 canvas2.C:114
 canvas2.C:115
 canvas2.C:116
 canvas2.C:117
 canvas2.C:118
 canvas2.C:119
 canvas2.C:120
 canvas2.C:121
 canvas2.C:122
 canvas2.C:123
 canvas2.C:124
 canvas2.C:125
 canvas2.C:126
 canvas2.C:127
 canvas2.C:128
 canvas2.C:129
 canvas2.C:130
 canvas2.C:131
 canvas2.C:132
 canvas2.C:133
 canvas2.C:134
 canvas2.C:135
 canvas2.C:136
 canvas2.C:137
 canvas2.C:138
 canvas2.C:139
 canvas2.C:140
 canvas2.C:141
 canvas2.C:142
 canvas2.C:143
 canvas2.C:144
 canvas2.C:145
 canvas2.C:146
 canvas2.C:147
 canvas2.C:148
 canvas2.C:149
 canvas2.C:150
 canvas2.C:151
 canvas2.C:152
 canvas2.C:153
 canvas2.C:154
 canvas2.C:155
 canvas2.C:156
 canvas2.C:157
 canvas2.C:158
 canvas2.C:159
 canvas2.C:160
 canvas2.C:161
 canvas2.C:162
 canvas2.C:163
 canvas2.C:164
 canvas2.C:165
 canvas2.C:166
 canvas2.C:167
 canvas2.C:168
 canvas2.C:169
 canvas2.C:170
 canvas2.C:171
 canvas2.C:172
 canvas2.C:173
 canvas2.C:174
 canvas2.C:175
 canvas2.C:176
 canvas2.C:177
 canvas2.C:178
 canvas2.C:179
 canvas2.C:180
 canvas2.C:181
 canvas2.C:182
 canvas2.C:183
 canvas2.C:184
 canvas2.C:185
 canvas2.C:186