# Grid, axis and "numbers" of a histogram

**URL:** <https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947>\
**Category:** ROOT\
**Created:** [January 23, 2008, 3:32pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947 "2008-01-23T15:32:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 23, 2008, 3:32pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/1 "2008-01-23T15:32:30Z")

</div>

Hi all,  
I do the TH2C histogram that is in attach.  
Now the numbers on Y axis are too many, I want 0, 4, 8,… but I want the grid for every number (0,1,2,3,4…).  
How should I do?

I use, for the grid, the code:

```auto
TCanvas *c1 = new TCanvas("c1","Visualizzazione",10,10,1000,500);
c1->SetGrid();

TH2C *h100 = new TH2C("h100","Visualizzazione",32,0.,32.,64,0.,64.);

h100->GetXaxis()->SetNdivisions(32);
h100->GetYaxis()->SetNdivisions(64); 
```

Thanks  
Ciccio

 ![](https://root-forum.cern.ch/uploads/default/original/2X/b/bebae298fb5aade6790ef54e48b63def110836cc.jpg)

---

<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:** [January 23, 2008, 5:03pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/2 "2008-01-23T17:03:14Z")

</div>

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

   TCanvas *c1 = new TCanvas("c1","Visualizzazione",10,10,1000,500);
   TH2C *h100 = new TH2C("h100","Visualizzazione",32,0.,32.,64,0.,64.);
   h100->GetXaxis()->SetNdivisions(32);
   h100->GetYaxis()->SetNdivisions(16);
   h100->Draw();

   TPad *grid = new TPad("grid","",0,0,1,1); 
   grid->Draw();
   grid->cd();
   grid->SetGrid();
   grid->SetFillStyle(4000); 

   TH2 *hgrid = new TH2C("hgrid","",32,0.,32.,64,0.,64.);   
   hgrid->Draw();
   hgrid->GetXaxis()->SetNdivisions(32);
   hgrid->GetYaxis()->SetNdivisions(64);
   hgrid->GetYaxis()->SetLabelOffset(999.); 
   hgrid->GetXaxis()->SetLabelOffset(999.); 

}
```

 ![](https://root-forum.cern.ch/uploads/default/original/2X/f/f159d7f6d5b4732c901afe2f303b9891f990d2a5.gif)

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 24, 2008, 3:41pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/3 "2008-01-24T15:41:57Z")

</div>

Hi cout,  
thank you for the answer, but now I don’t understand why I fill the histogram the grid don’t appears.  
I attach the plot.

the code is:

```auto
   TCanvas *c1 = new TCanvas("c1","Visualizzazione",10,10,1000,500);
   gStyle->SetOptStat(0);

   TFile *f = new TFile("visualizzazione.root","RECREATE");

   TH2C *h100 = new TH2C("h100","Visualizzazione",32,0.,32.,64,0.,64.);

   h100->GetXaxis()->SetNdivisions(32);
   h100->GetYaxis()->SetNdivisions(16); 
   h100->SetFillColor(3);
// h100->Draw();

   TPad *grid = new TPad("grid","",0,0,1,1);
   grid->Draw();
   grid->cd();
   grid->SetGrid();
   grid->SetFillStyle(4000);

   TH2C *hgrid = new TH2C("hgrid","",32,0.,32.,64,0.,64.);   
   hgrid->GetXaxis()->SetNdivisions(32);
   hgrid->GetYaxis()->SetNdivisions(64);
   hgrid->GetYaxis()->SetLabelOffset(999.);
   hgrid->GetXaxis()->SetLabelOffset(999.); 
   hgrid->Draw();

.......

    h100->Draw("BOX");
    hgrid->Draw("SAME");

```

thanks  
Ciccio

 ![](https://root-forum.cern.ch/uploads/default/original/2X/3/3f9b06227e46409af78d16f356de12fe68490dde.jpg)

---

<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:** [January 24, 2008, 3:56pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/4 "2008-01-24T15:56:14Z")

</div>

the grid is drawn in a transparent separated pad you should draw it after the histogram as I did in my 1st example.

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 24, 2008, 4:14pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/5 "2008-01-24T16:14:44Z")

</div>

> [@couet](#):
>
> the grid is drawn in a transparent separated pad you should draw it after the histogram as I did in my 1st example.

Sorry I don’t understad.  
Could you write the code, please?

---

<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:** [January 24, 2008, 4:16pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/6 "2008-01-24T16:16:14Z")

</div>

Set me your macro which fill the histogram.

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 24, 2008, 4:25pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/7 "2008-01-24T16:25:52Z")

</div>

> [@couet](#):
>
> Set me your macro which fill the histogram.

I attach the files that I use.  
thanks  
ciccio  
[visualizzazione.C](https://root-forum.cern.ch/uploads/default/original/2X/a/a544f12489b0cc624d090f13117946681663e47e.C) (2.69 KB)  
[conversione.h](https://root-forum.cern.ch/uploads/default/original/2X/b/baef372a70e071a5f28b7618cae2ed7d1a9e7bc3.h) (851 Bytes)

---

<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:** [January 24, 2008, 4:29pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/8 "2008-01-24T16:29:32Z")

</div>

I do not have : visualizzazione.root

---

<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:** [January 24, 2008, 4:30pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/9 "2008-01-24T16:30:13Z")

</div>

Sorry it is created n your macro … When I run your macro it crashes

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 24, 2008, 4:32pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/10 "2008-01-24T16:32:55Z")

</div>

> [@couet](#):
>
> Sorry it is created n your macro … When I run your macro it crashes

Ok, don’t worry  
thant you for all

Ciccio

---

<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:** [January 24, 2008, 4:34pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/11 "2008-01-24T16:34:20Z")

</div>

So we consider this item as solved ?

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 24, 2008, 4:48pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/12 "2008-01-24T16:48:00Z")

</div>

> [@couet](#):
>
> So we consider this item as solved ?

No solved, but suspended  
I continue to work and when I will solve it I will write the solution  
thanks

Ciccio

---

<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:** [January 24, 2008, 5:07pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/13 "2008-01-24T17:07:30Z")

</div>

That quite simple. The code whcih draw the grid is:

```auto
 TPad *grid = new TPad("grid","",0,0,1,1);
   grid->Draw();
   grid->cd();
   grid->SetGrid();
   grid->SetFillStyle(4000);

   TH2 *hgrid = new TH2C("hgrid","",32,0.,32.,64,0.,64.);   
   hgrid->Draw();
   hgrid->GetXaxis()->SetNdivisions(32);
   hgrid->GetYaxis()->SetNdivisions(64);
   hgrid->GetYaxis()->SetLabelOffset(999.);
   hgrid->GetXaxis()->SetLabelOffset(999.); 
```

It should be put after all the histogram drawings. And the histograms should be drawn without grid. This code you can even put in a function PaintGrid() and call it after all the histogram drawings.

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 24, 2008, 5:15pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/14 "2008-01-24T17:15:39Z")

</div>

[quote=“couet”]That quite simple. The code whcih draw the grid is:

```auto
 TPad *grid = new TPad("grid","",0,0,1,1);
   grid->Draw();
   grid->cd();
   grid->SetGrid();
   grid->SetFillStyle(4000);

   TH2 *hgrid = new TH2C("hgrid","",32,0.,32.,64,0.,64.);   
   hgrid->Draw();
   hgrid->GetXaxis()->SetNdivisions(32);
   hgrid->GetYaxis()->SetNdivisions(64);
   hgrid->GetYaxis()->SetLabelOffset(999.);
   hgrid->GetXaxis()->SetLabelOffset(999.); 
```

It should be put after all the histogram drawings. And the histograms should be drawn without grid. This code you can even put in a function PaintGrid() and call it after all the histogram drawings.[/quote]

I do it.

the last code rows are:

```auto
    h100->Draw("BOX");
// grid->Draw();
// hgrid->Draw();

   TPad *grid = new TPad("grid","",0,0,1,1);
   grid->Draw("SAME");
   grid->cd();
   grid->SetGrid();
   grid->SetFillStyle(4000);

   TH2C *hgrid = new TH2C("hgrid","",32,0.,32.,64,0.,64.);
   hgrid->Draw("SAME");
   hgrid->GetXaxis()->SetNdivisions(32);
   hgrid->GetYaxis()->SetNdivisions(64);
   hgrid->GetYaxis()->SetLabelOffset(999.);
   hgrid->GetXaxis()->SetLabelOffset(999.); 

```

and the result is only the grid, the histogram empty

---

<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:** [January 25, 2008, 8:46am UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/15 "2008-01-25T08:46:37Z")

</div>

Why have you added the option SAME in two places ? that was not what I sent you.  
O,k if at some point you end up with something I can run, I will look at it.

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 30, 2008, 11:49am UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/16 "2008-01-30T11:49:56Z")

</div>

Hi couet,  
my apologies for the delayed response.  
I send you a simple example.

```auto
{

   TCanvas *c1 = new TCanvas("c1","Visualizzazione",10,10,1000,500);
   gStyle->SetOptStat(0);

   TH2C *h100 = new TH2C("h100","Visualizzazione",32,0.,32.,64,0.,64.);

for (int i=0; i<64; i++)
{
   h100->Fill(25,i,1);
}

   h100->GetXaxis()->SetNdivisions(32);
   h100->GetYaxis()->SetNdivisions(16);
 h100->SetFillColor(3);
// h100->Draw("BOX");

   TPad *grid = new TPad("grid","",0,0,1,1);
   grid->Draw();
   grid->cd();
   grid->SetGrid();
   grid->SetFillStyle(4000);

   TH2 *hgrid = new TH2C("hgrid","",32,0.,32.,64,0.,64.);   
   hgrid->Draw();
   hgrid->GetXaxis()->SetNdivisions(32);
   hgrid->GetYaxis()->SetNdivisions(64);
   hgrid->GetYaxis()->SetLabelOffset(999.);
   hgrid->GetXaxis()->SetLabelOffset(999.);

   h100->Draw("BOX");

}

```

If I write ` h100->Draw("BOX"); ` after ` h100->SetFillColor(3); ` I obtain only the grid, like figure c1.gif,  
if I write where it is now, I obtain the figure prova1.jpg.  
Why?

I would the figure prova1.jpg with grid of figure c1.gif.

Thank you  
Ciccio

---

<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:** [January 30, 2008, 12:36pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/17 "2008-01-30T12:36:37Z")

</div>

Here it is:  
[grid3.C](https://root-forum.cern.ch/uploads/default/original/2X/b/bd9ed01bca115ac8e0b39a41d65b049c20df94b4.C) (809 Bytes)

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 30, 2008, 1:51pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/18 "2008-01-30T13:51:23Z")

</div>

It’s perfect!

Thank you  
Ciccio

P.S.: Why don’t work if I write all in a single function?

---

<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:** [January 30, 2008, 1:55pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/19 "2008-01-30T13:55:04Z")

</div>

one fill style was missing. Compare with the original code.

---

<div class="post-metadata">

**Author:** ![cicciodevoto](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@cicciodevoto](https://root-forum.cern.ch/u/cicciodevoto)\
**Post date:** [January 30, 2008, 2:07pm UTC](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947/20 "2008-01-30T14:07:04Z")

</div>

> [@couet](#):
>
> one fill style was missing. Compare with the original code.

OK, I understad.  
Thank you for the aid

Ciccio

[Next page](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947.md?page=2)
