# Offset in height for a TH2

**URL:** <https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865>\
**Category:** Newbie\
**Created:** [April 14, 2020, 1:24am UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865 "2020-04-14T01:24:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [April 14, 2020, 1:24am UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/1 "2020-04-14T01:24:44Z")

</div>

Hello,

I want to draw one 2D histogram that doesn’t start at 0 when the bin is empty, but at a fixed height on the z axis, let’s say z0, so that the space between 0 an z0 in the z axis is empty.

The reason I want to do this is that I want to draw some (let’s say two) 2D histograms in the same pad, separated in the z axis by a given value.

I thought this could be the best way to do so, and then overlapping the histograms with this offset but I couldn’t even find a way to set this offset for one histogram.

To be short, I need a command like “myhist-\>StartFrom(z0)”

Apologies in advance if this is too basic, I would appreatiate your help on this problem.

ROOT Version: 6.14/04  
Platform: Ubuntu

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2020, 6:11am UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/2 "2020-04-14T06:11:51Z")

</div>

`myhist->SetMinimum(z0);`

---

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [April 14, 2020, 1:57pm UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/3 "2020-04-14T13:57:02Z")

</div>

This is what I thought at first, but it only hides the bins with height \<z0, which re-appear when I unzoom. I need that the histogram starts filling from z0 so that when I unzoom there’s a blank space between 0 and z0.

Thanks for your support,

Julian.

---

<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:** [April 14, 2020, 2:49pm UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/4 "2020-04-14T14:49:01Z")

</div>

The following code will do the vertical shift of a TH2:

```auto
void addconstth2( TH2F* h, float z) {
   int nbinx = h->GetNbinsX();
   int nbiny = h->GetNbinsY();
   for (int i=1; i<=nbinx; i++) {
      for (int j=1; j<=nbiny; j++) {
         h->SetBinContent(i,j,h->GetBinContent(i,j)+z);
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [April 14, 2020, 9:25pm UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/5 "2020-04-14T21:25:55Z")

</div>

This code is really helphul, thanks! But it’s still not what I have in mind, sorry if I wasn’t clear on that.

The code does boost the hight of every element of the histogram to the desired z0, but it’s still visible from 0 to z0. I’m lookin forward to get something like this:

 ![BoostZ2D](https://root-forum.cern.ch/uploads/default/original/3X/7/4/74bc499935cd0eb6bdb7ce848c93cc0617a0e40e.png)

where the white bins should be nothing but blank space.

(sorry if I took a while to respond, but I was trying to solve it myself with your code).

Thanks again for the help.

---

<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:** [April 15, 2020, 6:23am UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/6 "2020-04-15T06:23:51Z")

</div>

I do not see an easy way how to make this space completely empty …  
Using THStack you will get something similar to the picture you sent. Ie: not empty  
may be a 3D histogram draw with option box can provide something more useful for you … not sure … I do not have ideas right now.

---

<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:** [April 15, 2020, 4:00pm UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/7 "2020-04-15T16:00:15Z")

</div>

Using THSack and a transparent level I can do the attached picture. But that does not really work as the histogram at the bottom is on partially drawn.

 ![Screenshot 2020-04-15 at 17.57.59](https://root-forum.cern.ch/uploads/default/original/3X/1/5/15e482e8df9a3ac52cb036b3be8baee32c4bcb2e.png)

---

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [April 15, 2020, 4:48pm UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/8 "2020-04-15T16:48:31Z")

</div>

Yes, I usted THStack to make the figure above, I used the blank bins to make myself more clear, but later I could make them transparent, all with help of your code.

But, as you pointed out yourself, all histograms in the bottom will be incomplete because they have a transparent histogram on top of if.

I think this is good enough though, so thanks for your help!

But I’ll keep this open in case that someone comes up with the solution to this problem.

---

<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:** [April 16, 2020, 6:25am UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/9 "2020-04-16T06:25:05Z")

</div>

The macro producing the previous plot is:

```auto
TH2F *spaceabove(TH2F *h, float s, TString ht, int c) {
   int nbinx = h->GetNbinsX();
   int nbiny = h->GetNbinsY();
   float x1 = h->GetXaxis()->GetXmin();
   float x2 = h->GetXaxis()->GetXmax();
   float y1 = h->GetYaxis()->GetXmin();
   float y2 = h->GetYaxis()->GetXmax();

   auto hs = new TH2F(ht.Data(), ht.Data(),nbinx, x1, x2, nbiny, y1 , y2);

   for (int i=1; i<=nbinx; i++) {
      for (int j=1; j<=nbiny; j++) {
         hs->SetBinContent(i,j,s-h->GetBinContent(i,j));
      }
   }
   hs->SetFillColor(c);
   return hs;
}

void flyinglegoplots() {

   Int_t ci = 1756;
   auto *c = new TColor(ci, 1., 0., 0.3, "", 0.);
   Int_t dark = TColor::GetColorDark(ci);
   auto *cd = gROOT->GetColor(dark);
   cd->SetAlpha(0.);
printf("dark = %d\n",dark);

   THStack *a = new THStack("a","Stacked 2D histograms");

   TF2 *f1 = new TF2("f1", "xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
   Double_t params[] = {130,-1.4,1.8,1.5,1, 150,2,0.5,-2,0.5, 3600,-2,0.7,-3,0.3};
   f1->SetParameters(params);

   TH2F *h2sta = new TH2F("h2sta","h2sta",20,-4,4,20,-4,4);
   h2sta->SetFillColor(38);
   h2sta->FillRandom("f1",4000);

   TF2 *f2 = new TF2("f2","xygaus + xygaus(5)",-4,4,-4,4);
   Double_t params2[] = {100,-1.4,1.9,1.1,2, 80,2,0.7,-2,0.5};
   f2->SetParameters(params2);
   TH2F *h2stb = new TH2F("h2stb","h2stb",20,-4,4,20,-4,4);
   h2stb->SetFillColor(46);
   h2stb->FillRandom("f2",3000);

   a->Add(h2sta);
   a->Add(spaceabove(h2sta, 50, "hs1",ci));
   a->Add(h2stb);

   a->Draw("lego3");
}

```

---

<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:** [April 16, 2020, 8:53am UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/10 "2020-04-16T08:53:08Z")

</div>

An other approach:

```auto
{
   gStyle->SetOptStat(0);
   gStyle->SetHistTopMargin(0);

   auto f1 = new TF2("f1", "xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
   Double_t params[] = {130,-1.4,1.8,1.5,1, 150,2,0.5,-2,0.5, 3600,-2,0.7,-3,0.3};
   f1->SetParameters(params);
   auto h2sta = new TH2F("h2sta","",20,-4,4,20,-4,4);
   h2sta->SetFillColor(38);
   h2sta->FillRandom("f1",4000);

   auto f2 = new TF2("f2","xygaus + xygaus(5)",-4,4,-4,4);
   Double_t params2[] = {100,-1.4,1.9,1.1,2, 80,2,0.7,-2,0.5};
   f2->SetParameters(params2);
   auto *h2stb = new TH2F("h2stb","",20,-4,4,20,-4,4);
   h2stb->SetFillColor(46);
   h2stb->FillRandom("f2",3000);

   auto C = new TCanvas();

   double ph = 0.5;
   double p2y = 0.22;

   double max = h2stb->GetMaximum()*1.5;

   h2sta->SetMaximum(max);
   h2stb->SetMaximum(max);

   C->cd();
   auto *pad1 = new TPad("pad1","",0,0,1,ph);
   pad1->SetFillStyle(4000);
   pad1->Draw(); pad1->cd(); h2sta->Draw("lego1 fb ");

   C->cd();
   auto *pad2 = new TPad("pad2","",0,p2y,1,p2y+ph);
   pad2->SetFillStyle(4000);
   h2stb->GetXaxis()->SetLabelSize(0); h2stb->GetYaxis()->SetLabelSize(0);
   h2stb->GetXaxis()->SetTickSize(0); h2stb->GetYaxis()->SetTickSize(0);
   pad2->Draw(); pad2->cd(); h2stb->Draw("lego1 fb ");

}

```

 ![Screenshot 2020-04-16 at 10.52.11](https://root-forum.cern.ch/uploads/default/original/3X/b/6/b6830d7ca172e4a8ba85fadce0b63a54591fb56f.png)

---

<div class="post-metadata">

**Author:** ![juliangb](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/juliangb/32/21448_2.png) [@juliangb](https://root-forum.cern.ch/u/juliangb)\
**Post date:** [April 17, 2020, 4:15pm UTC](https://root-forum.cern.ch/t/offset-in-height-for-a-th2/38865/11 "2020-04-17T16:15:44Z")

</div>

Last one does the trick, thanks!
