# Axes and grid with different divisions

**URL:** <https://root-forum.cern.ch/t/axes-and-grid-with-different-divisions/44979>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [May 19, 2021, 5:29pm UTC](https://root-forum.cern.ch/t/axes-and-grid-with-different-divisions/44979 "2021-05-19T17:29:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CaueEvangelista](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@CaueEvangelista](https://root-forum.cern.ch/u/CaueEvangelista)\
**Post date:** [May 19, 2021, 5:29pm UTC](https://root-forum.cern.ch/t/axes-and-grid-with-different-divisions/44979/1 "2021-05-19T17:29:01Z")

</div>

Hello all!

This is a very naive issue.

I’m trying to plot a 2D histogram with a grid. My histogram covers a region from -0.5 to 0.5, both in eta and phi. My original ideia was to set a 0.02 x 0.02 grid, and so I created 50 divisions to each axis.

The problem is: now I have 50 labels in the axes as well. My intention was to maintain 10 labels in each axis (-0.5,-0.4,…,etc) but keep the 0.02x0.02 grid.

I know that this problem was fixed in a post([Grid, axis and "numbers" of a histogram](https://root-forum.cern.ch/t/grid-axis-and-numbers-of-a-histogram/5947)) using a TPad grid, but I really did not understand the parameters of the grid there.

Here is my code and the plot:

```auto
#include <iostream>
#include <fstream>
#include <cmath>

#ifdef __CLING__
R__LOAD_LIBRARY(libDelphes)
#include "classes/DelphesClasses.h"
#include "external/ExRootAnalysis/ExRootTreeReader.h"
#endif

void transf_deposition(){
	gSystem->Load("libDelphes");

    TChain chain("Delphes");
 	chain.Add("tag_1_delphes_events500.root");

 	// Create object of class ExRootTreeReader
    ExRootTreeReader *treeReader = new ExRootTreeReader(&chain);
	TClonesArray *branchJet = treeReader->UseBranch("Jet");
	TClonesArray *branchTower = treeReader->UseBranch("Tower");
	TClonesArray *branchParticle = treeReader-> UseBranch ("Particle");

	//Declara o canvas
    TCanvas *c1 = new TCanvas("c1","c1",200,200);
    c1->SetLeftMargin(0.15);
    c1->SetFillColor(13);
    c1 -> SetGrid();

    //Declare the lego histogram
    TH2F *hist_lego = new TH2F("hist_lego","#eta x #phi x E", 50, -0.5, 0.5,50, -0.5, 0.5);
    hist_lego -> GetXaxis()->SetTitle("#eta");
    hist_lego -> GetYaxis()->SetTitle("#phi");
    hist_lego -> GetZaxis()->SetTitle("E");

    hist_lego -> GetXaxis()->CenterTitle();
    hist_lego -> GetYaxis()->CenterTitle();
    hist_lego -> GetZaxis()->CenterTitle();

    hist_lego -> GetXaxis() -> SetNdivisions(50);
   	hist_lego -> GetYaxis() -> SetNdivisions(50);

   	/*
		Lines that fill the histogram
   	*/

	gStyle->SetPalette(kBird);
	hist_lego ->SetStats(0);

	hist_lego->Draw("colz");

	c1 -> Print("/home/ecaue/ParticlePhysics/TCC/Codes/Transformações/deposition.pdf");

}

```

 ![plot](https://root-forum.cern.ch/uploads/default/original/3X/0/1/01282751489467087878e384ff34e312235072eb.png)

---

<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:** [May 20, 2021, 7:38am UTC](https://root-forum.cern.ch/t/axes-and-grid-with-different-divisions/44979/2 "2021-05-20T07:38:18Z")

</div>

> [@CaueEvangelista](#):
>
> The problem is: now I have 50 labels in the axes as well. My intention was to maintain 10 labels in each axis (-0.5,-0.4,…,etc) but keep the 0.02x0.02 grid.

The dotted grid lines are drawn on the main tick marks like the axis labels.  
I made an example showing how to get what you are looking for.

#### customgrid.C :

```auto
void drawgrid()
{
   double xs = 0.2; // grid step along X
   double ys = 0.2; // grid step along Y
   double xmin,ymin,xmax,ymax;
   gPad->GetRangeAxis(xmin,ymin,xmax,ymax);
   auto aline = new TLine();
   aline->SetLineStyle(3);
   for (double yg = ymin+ys; yg < ymax; yg=yg+ys) aline->PaintLine(xmin,yg,xmax,yg);
   for (double xg = xmin+xs; xg < xmax; xg=xg+xs) aline->PaintLine(xg,ymin,xg,ymax);
}

void customgrid()
{
   gStyle->SetOptStat(0);
   auto h = new TH2D("h","",10,0,10,10,0,10);
   h->Draw();
   h->GetXaxis()->SetRangeUser(2,8);
   gPad->Update();

   auto grid = new TExec("grid","drawgrid()");
   grid->Draw();
}

```

 ![Screenshot 2021-05-20 at 09.36.28](https://root-forum.cern.ch/uploads/default/original/3X/d/0/d0302f5623b9985878aea7b7d01087ac8554363f.png)
