Grid, axis and "numbers" of a histogram

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:

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


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

}


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:

   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


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?

Set me your macro which fill the histogram.

I attach the files that I use.
thanks
ciccio
visualizzazione.C (2.69 KB)
conversione.h (851 Bytes)

I do not have : visualizzazione.root

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

Ok, don’t worry
thant you for all

Ciccio

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

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

 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=“couet”]That quite simple. The code whcih draw the grid is:

 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:

    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

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.

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

{

   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

Here it is:
grid3.C (809 Bytes)

It’s perfect!

Thank you
Ciccio

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

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

OK, I understad.
Thank you for the aid

Ciccio