Offset in height for a TH2

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

myhist->SetMinimum(z0);

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.

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

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);
      }
   }
}
1 Like

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:

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.

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.

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.

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.

The macro producing the previous plot is:

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

An other approach:

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

}

1 Like

Last one does the trick, thanks!