Drawing several histograms with Divide()

Hello,

Hope someone can help me with the following problems.

First Problem:
I want to plot several histograms (4x3) into one Canvas without spaces between them. I’m using c1->Divide(4,3,0,0) for this, which works fine but the axis labels become very small. If I increase their size by
hist->GetXaxis()->SetLabelFont(63);
hist->GetXaxis()->SetLabelSize(15);
so that they are readable they range over the canvas size.
Does anyone know how to increase the left margin of the canvas so that the bigger labels and axis titles fit? SetLeftMargin for the canvas does’t work at all and if I specify a left margin only for the ‘left side’ pads they become smaller than the other ones (see attachments).

Second problem:
Although I have the same range on the y-axis the tick marks of two adjacent histograms don’t fit. Any idea what’s wrong?

Third question:
Is their a global canvas title or do only the pads have titles? I would like to give some global information about the data I’m plotting as a kind of headline.

Thanks in advance

Petra

Script:

void spr_colz_iso()
{

gROOT->SetStyle(“Plain”);
gStyle->SetPalette(1);
gStyle->SetOptStat(1);
gStyle->SetOptFit(1);
gStyle->SetLineWidth(1);
gStyle->SetTextFont(63);
gStyle->SetTextSize(15);
gStyle->SetLabelSize(20,“xy”);
gStyle->SetLabelFont(63,“xy”);
gStyle->SetOptTitle(0);
// gStyle->SetTitleSize(5.0);
// gStyle->SetLabelSize(5);
gStyle->SetLineWidth(1);

//----------------------------------------------------------------------------
// Total SPR in calorimeter parts
//----------------------------------------------------------------------------

// gStyle->SetOptFit(1110);
// gStyle->SetOptStat(0);

int num = 0;
int iiso = 3;
char histname[1000];
char prjname[1000];

TCanvas *SPRTotC = new TCanvas(“SPRTotC”,“SPR in em/fh/ch in CC/ICR/EC”,900,600);
SPRTotC->Divide(4,3,0,0);

for (int ical = 0; ical < 3; ical++)
{
for (int idet = 0; idet < 4; idet++)
{
num++;
SPRTotC->cd(num);
gPad->SetTicks(1,1);
sprintf(histname, “ha_spr_tot_mu_cal%d_det%d_iso%d”, ical, idet, iiso);
TH2F* hist = (TH2F*)gROOT->FindObject(histname);
hist->GetXaxis()->SetRangeUser(0.0,50.0);
hist->GetXaxis()->SetNdivisions(205,0);
hist->GetXaxis()->SetLabelFont(63);
hist->GetXaxis()->SetLabelSize(15);
hist->GetXaxis()->SetTitleFont(63);
hist->GetXaxis()->SetTitleSize(15);
hist->GetXaxis()->SetTitleOffset(1.8);

  hist->GetYaxis()->SetRangeUser(0.0,5.0);
  hist->GetYaxis()->SetNdivisions(205,0);
  hist->GetYaxis()->SetLabelFont(63);
  hist->GetYaxis()->SetLabelSize(15);
  hist->GetYaxis()->SetTitleFont(63);
  hist->GetYaxis()->SetTitleSize(15);
  hist->GetYaxis()->SetTitleOffset(2.5);
  SPRTotC->Update();
  hist->Draw("colz");
}

}
}




[quote]First Problem:
I want to plot several histograms (4x3) into one Canvas without spaces between them. I’m using c1->Divide(4,3,0,0) for this, which works fine but the axis labels become very small. If I increase their size by
hist->GetXaxis()->SetLabelFont(63);
hist->GetXaxis()->SetLabelSize(15);
so that they are readable they range over the canvas size.
Does anyone know how to increase the left margin of the canvas so that the bigger labels and axis titles fit? SetLeftMargin for the canvas does’t work at all and if I specify a left margin only for the ‘left side’ pads they become smaller than the other ones (see attachments).
[/quote]

You are likely using an old version. In recent versions (less than one year)
you should get identical pad sizes when setting the margins.

[quote]Second problem:
Although I have the same range on the y-axis the tick marks of two adjacent histograms don’t fit. Any idea what’s wrong?
[/quote]

I cannot reproduce this problem. We will need a concrete running script showing the problem.

[quote]Third question:
Is their a global canvas title or do only the pads have titles? I would like to give some global information about the data I’m plotting as a kind of headline.
[/quote]

see example in tutorial zdemo.C

Rene

Hello Rene,

thanks for your fast answer. The tutorial is exactly what I need for the headline. I only found the zones.C tutorial myself.

To the other problems:
I’m using Root Version 4.03/05 from april this year, which is the latest one we have installed. The pad size is ok, if I’m using divide(), but the problem is to increase the left margin so that the axis titles and labels fit if I use a bigger font.
If I increase the left margin for the 3 pads on the left - as they contain the axis titles and labels - they become smaller than the other ones (see outcommented line in the script). If I try to increase the canvas left margin it doesn’t work. But even if it would work the axis titles would probably be cut again as they ar part of the pads, correct?
I will attach a Root tree and a short script so that you can reproduce also the problem with the tickmarks. The picture it produces was attached yesterday.

By the way, I forgot to mention another problem. As you can see from the plots the legend containing the colortable is merely visible. Can you tell me how to increase the size? And is there a possibility to set the range of the colors (minimum, maximum counts) so that it is the same for all plots? If it is possible, can one then have only one (canvas-size) legend for all plots at the right side? That would make reading much easier.

Bye, Petra
spr_test.C (1.84 KB)
PionTree_dat_test.root (767 KB)

The example you sent does not run. I get:

root [0] .x spr_test.C
Error: illegal pointer to class object hist 0x0 373  FILE:spr_test.C LINE:44
*** Interpreter error recovered ***
root [1]

Can you send a running example ?

Hello,

if I do

root PionTree_dat_test.root
root [0]
Attaching file PionTree_dat_test.root as _file0…
root [1] .x spr_test.C

the script works fine. I don’t know what to change. I’m using Root 4.03/05.

Bye, Petra

Yes you are right. Sorry. I will look at your problem asap.

I think this one is better:

void spr_test()
{
   gROOT->SetStyle("Plain");
   gStyle->SetPalette(1);
   gStyle->SetOptStat(1);
   gStyle->SetOptFit(1);
   gStyle->SetLineWidth(1);
   gStyle->SetTextFont(63);
   gStyle->SetTextSize(15);
   gStyle->SetLabelSize(20,"xy");
   gStyle->SetLabelFont(63,"xy");
   gStyle->SetOptTitle(0);
                                                                                
  //----------------------------------------------------------------------------  
  / Total SPR in calorimeter parts
  //----------------------------------------------------------------------------                                                                                
   int num = 0;
   int iiso = 2;
   char histname[1000];
   char prjname[1000];
                                                                                
   TCanvas *SPRTotC = new TCanvas("SPRTotC","SPR",900,600);
   SPRTotC->SetLeftMargin(0.2);
   SPRTotC->Divide(4,3,0);
                                                                                
   for (int ical = 0; ical < 3; ical++) {
      for (int idet = 0; idet < 4; idet++) {
         num++;
         gPad->SetTicks(1,1);
         sprintf(histname, "ha_spr_tot_pi_cal%d_det%d_iso%d", ical, idet, iiso);
         TH2F* hist = (TH2F*)gROOT->FindObject(histname);
         hist->GetXaxis()->SetRangeUser(0.0,50.0);
         hist->GetXaxis()->SetNdivisions(205,0);
         hist->GetXaxis()->SetLabelFont(63);
         hist->GetXaxis()->SetLabelSize(15);
         hist->GetXaxis()->SetTitleFont(63);
         hist->GetXaxis()->SetTitleSize(15);
         hist->GetXaxis()->SetTitleOffset(1.8);
         hist->GetYaxis()->SetRangeUser(0.0,5.0);
         hist->GetYaxis()->SetNdivisions(205,0);
         hist->GetYaxis()->SetLabelFont(63);
         hist->GetYaxis()->SetLabelSize(15);
         hist->GetYaxis()->SetTitleFont(63);
         hist->GetYaxis()->SetTitleSize(15);
         hist->GetYaxis()->SetTitleOffset(2.5);
         SPRTotC->cd(num);
         hist->Draw("col");
      }
   }
}
1 Like

This helps a lot! Thank you! :smiley:

But I don’t understand what makes the difference. As far as I can see the only new thing is that Divide() is called with three arguments instead of four. I can’t find any documentation for that. In the root reference guide I only found the Tpad source code where the method

void TPad::Divide(Int_t nx, Int_t ny, Float_t xmargin, Float_t ymargin, Int_t color)

exists. There are four arguments given. Can you explain? And what was wrong with the tickmarks?

Thanks again for your help.

Petra

PS: Will you have a look at the legend problem also?

Yes I can explain :slight_smile:

Sorry to have changed the Divide calling sequence. It was not necessary. It end up like that after many eddting I did. In fact the important changes are:

  1. SPRTotC->SetLeftMargin(0.2); // to set the global left margin

  2. remove the individual left margin settings on the 4 left pads (in the loop)

  3. suppress the Z option in Draw. The palettes are not visible anyway and the misaligned ticks came from that.

PS: i will look at the legend also

I see…
but that’s mainly what I tried before, that’s why the leftmargin commands for the individual pads were outcommented. The problem is, that the pad sizes are not correctly calculated. I’ll attach a plot with large margins to show this problem. The left pads are still smaller than the other ones. For the bottom ones this problem also exists but it is smaller because of the landscape page.
The problem seems to have improved with the global margins instead of pad margins but it still exists. Do you think it’s a problem in the pad size calculation of Divide?

I used
SPRTotC->SetLeftMargin(0.3);
SPRTotC->SetRightMargin(0.15);
SPRTotC->SetTopMargin(0.3);
SPRTotC->SetBottomMargin(0.3);
SPRTotC->Divide(4,3,0,0);
to produce the plot.

Bye, Petra