Need help to draw six plots using multi canvas

Dear Experts,

I am trying to draw six plots using multi canvas using the below code. When I write something on each pad that you could see in L101-L106 of the code, I noticed that the text size and position were not the same inside each pad. Also, inside each pad, I wanted to draw another small pad, which you can see in L108-L112 of the code. The pads are drawn in unequal sizes. Could you please help me with this?

canvas2.C (5.5 KB)

Thank you for your help.

Best regards,
Raghunath

Welcome to the ROOT forum.

I think you stated from this example: ROOT: tutorials/graphics/canvas2.C File Reference
In that example the plots have the same sizes but not the pads. The pads on left are bigger because they contain the margins. You need to have special case for the pads on left.

Dear @couet,

Yes, I copied the code from there. There is no other way to adjust the pad size? If not, maybe I need to adjust pad size manually. Same thing is happening for the text size. The text size I can manage by doing globally.

Thank you for your help.

Best regards,
Raghunath

if you use Divide all the pad will have the same size, but the plots won’t

Dear @couet,

I find the same problem with the divide option like canvas->Divide(3,2,0,0). In this case, the label size also changes for the upper and lower panels.

Best regards,
Raghunath

Here we are:

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()
{

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

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

   const char *cc[Nx][Ny];
   cc[0][1] = {"0-10%"};
   cc[1][1] = {"10-20%"};
   cc[2][1] = {"20-30%"};
   cc[0][0] = {"30-40%"};
   cc[1][0] = {"40-50%"};
   cc[2][0] = {"50-60%"};

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

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

         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(1);
         hFrame->GetXaxis()->CenterTitle();
         hFrame->GetXaxis()->SetNdivisions(505);

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

         h->Draw("same");

         double xl,yl,xu,yu;
         gPad->GetPadPar(xl,yl,xu,yu);
         double pw = xu-xl;
         double ph = yu-yl;

         double lm = gPad->GetLeftMargin();
         double rm = gPad->GetRightMargin();
         double tm = gPad->GetTopMargin();
         double bm = gPad->GetBottomMargin();

         double fw = pw-pw*lm-pw*rm;
         double fh = ph-ph*bm-ph*tm;

         double tx = (0.4*fw+pw*lm)/pw;
         double ty = (0.8*fh+bm*ph)/ph;

         TLatex * text = new TLatex(tx,ty, cc[i][j]);
         text->SetNDC();
         text->SetTextAlign(11);
         text->SetTextFont(43);
         text->SetTextSize(10);
         text->Draw();

         double x1 = (0.2*fw+pw*lm)/pw;
         double x2 = (0.8*fw+pw*lm)/pw;
         double y1 = (0.2*fh+bm*ph)/ph;
         double y2 = (0.7*fh+bm*ph)/ph;

         TPad* pad = new TPad(Form("pad_%d%d", i, j), "", x1,y1,x2,y2);
         pad->Draw();
         pad->cd();
         TH2D* h2d = new TH2D(Form("h2d_%d%d", i, j), "", 500, 0, 10, 400, 0.8, 2.6);
         h2d->Draw();
      }
   }

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

Dear @couet,

Thank you very much for your help. I also have managed manually. But I would prefer your code. Thank you.

Best regards,
Raghunath

I made this PR to improve le canvas2.C tutorial. Thanks for pointing out this issue.

Thank you very much, @couet. This would be very helpful for all.

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