# Drawing several histograms with Divide()

**URL:** <https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115>\
**Category:** ROOT\
**Created:** [July 5, 2005, 6:23pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115 "2005-07-05T18:23:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![haefner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/87869e/32.png) [@haefner](https://root-forum.cern.ch/u/haefner)\
**Post date:** [July 5, 2005, 6:23pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/1 "2005-07-05T18:23:08Z")

</div>

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)](https://root-forum.cern.ch/uploads/default/original/1X/fe5f42ae79e389360ae9d969db4dcc4bca03d384.gif "Cool");

```
  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");
}

```

}  
}

 ![](https://root-forum.cern.ch/uploads/default/original/1X/566dce2d172eebe9a1db7deddb97e5ede0bd8b15.gif)  
  
 ![](https://root-forum.cern.ch/uploads/default/original/1X/5976aba254dbab8ff453ac34ff0218b96a79a8b3.gif)

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 6, 2005, 8:01am UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/2 "2005-07-06T08:01:34Z")

</div>

[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

---

<div class="post-metadata">

**Author:** ![haefner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/87869e/32.png) [@haefner](https://root-forum.cern.ch/u/haefner)\
**Post date:** [July 6, 2005, 10:08am UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/3 "2005-07-06T10:08:54Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/1X/fcaa52eee5c701f65eab51c19f79fc19f1edeb1f.C) (1.84 KB)  
[PionTree\_dat\_test.root](https://root-forum.cern.ch/uploads/default/original/1X/dca84839b38e97315ff73d51a21e8c14aeac505b.root) (767 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 6, 2005, 12:16pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/4 "2005-07-06T12:16:41Z")

</div>

The example you sent does not run. I get:

```auto
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 ?

---

<div class="post-metadata">

**Author:** ![haefner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/87869e/32.png) [@haefner](https://root-forum.cern.ch/u/haefner)\
**Post date:** [July 7, 2005, 8:20am UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/5 "2005-07-07T08:20:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 7, 2005, 8:23am UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/6 "2005-07-07T08:23:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 7, 2005, 2:48pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/7 "2005-07-07T14:48:23Z")

</div>

I think this one is better:

```auto
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");
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![haefner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/87869e/32.png) [@haefner](https://root-forum.cern.ch/u/haefner)\
**Post date:** [July 7, 2005, 3:18pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/8 "2005-07-07T15:18:20Z")

</div>

This helps a lot! Thank you! 😃

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?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 7, 2005, 3:31pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/9 "2005-07-07T15:31:02Z")

</div>

Yes I can explain 🙂

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

---

<div class="post-metadata">

**Author:** ![haefner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/87869e/32.png) [@haefner](https://root-forum.cern.ch/u/haefner)\
**Post date:** [July 7, 2005, 4:02pm UTC](https://root-forum.cern.ch/t/drawing-several-histograms-with-divide/2115/10 "2005-07-07T16:02:35Z")

</div>

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

 ![](https://root-forum.cern.ch/uploads/default/original/1X/032a1a86f2cdf10ee09fdbae3adefbac7f100b07.gif)
