# Setting axes on TGraph2D (cont 4z)

**URL:** <https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284>\
**Category:** Newbie\
**Tags:** graphics\
**Created:** [March 5, 2020, 7:02pm UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284 "2020-03-05T19:02:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rms](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rms/32/26589_2.png) [@rms](https://root-forum.cern.ch/u/rms)\
**Post date:** [March 5, 2020, 7:02pm UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/1 "2020-03-05T19:02:34Z")

</div>

Hi, I am having trouble setting my axes on a TGraph2D. On the forums they mentioned that I could use SetHistogram(), but I don’t know how to use that. All this code is in PyROOT, but the only difference is that pointers are just periods.

```
c2 = TCanvas( 'c2','Densities',800,800)

gr_dens = TGraph2D()
gr_dens.SetTitle("Energy Density Plot;""x [fm];""y f[m];"" [GeV fm^-3]")

N = 0
for i in range(0,100):
    for j in range(0,100):
        x_gr_dens = x_grid[i, j]
        y_gr_dens = y_grid[i, j]
        rho_gr_dens = rho[i,j]*conv
        gr_dens.SetPoint(N, x_gr_dens, y_gr_dens, rho_gr_dens)
        N +=1

gStyle.SetPalette(55)  
gr_dens.Draw("cont4z")
```

---

<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:** [March 6, 2020, 8:26am UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/2 "2020-03-06T08:26:47Z")

</div>

> [@rms](#):
>
> I am having trouble setting my axes on a TGraph2D

You mean changing the axis limits ? which axis ?

---

<div class="post-metadata">

**Author:** ![rms](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rms/32/26589_2.png) [@rms](https://root-forum.cern.ch/u/rms)\
**Post date:** [March 6, 2020, 3:31pm UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/3 "2020-03-06T15:31:13Z")

</div>

Sorry, yes, I mean axis limits. I would like to set axis limits for the x and y axes on the cont4z plot, but this is difficult. I tried the code below and it did not work.

axisX = gr\_dens.GetXaxis()  
axisX.SetLimits(-14,14)

---

<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:** [March 6, 2020, 3:46pm UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/5 "2020-03-06T15:46:56Z")

</div>

```auto
void graph2cont4z() {
   TCanvas *c1 = new TCanvas("c1");

   Double_t rnd, x, y, z;
   Double_t e = 0.3;
   Int_t nd = 500;

   TRandom r;
   TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
   f2->SetParameters(1,1);
   auto gr2 = new TGraph2D(nd);

   // Fill the 2D graph
   Double_t zmax = 0;
   for (Int_t i=0; i<nd; i++) {
      f2->GetRandom2(x,y);
      rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
      z = f2->Eval(x,y)*(1+rnd);
      if (z>zmax) zmax = z;
      gr2->SetPoint(i,x,y,z);
   }
   gr2->Draw("cont4 z");
   TH2D *h2 = gr2->GetHistogram();
   h2->GetXaxis()->SetRangeUser(-2,2);
   h2->GetYaxis()->SetRangeUser(-2,2);
}

```

---

<div class="post-metadata">

**Author:** ![rms](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rms/32/26589_2.png) [@rms](https://root-forum.cern.ch/u/rms)\
**Post date:** [March 10, 2020, 6:58pm UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/6 "2020-03-10T18:58:59Z")

</div>

Thanks! I’m still struggling with it, but I’ll get there.

---

<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:** [March 11, 2020, 9:25am UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/7 "2020-03-11T09:25:44Z")

</div>

Seems to me the macro I sent you answers your question. What is your problem ?

---

<div class="post-metadata">

**Author:** ![rms](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rms/32/26589_2.png) [@rms](https://root-forum.cern.ch/u/rms)\
**Post date:** [March 12, 2020, 3:59am UTC](https://root-forum.cern.ch/t/setting-axes-on-tgraph2d-cont-4z/38284/8 "2020-03-12T03:59:58Z")

</div>

Sorry it took me a while to respond. I actually ended up using a TH2D instead of a TGraph2D (I had to use that to draw several graphs on the same axis using Draw(“SAME”). I did manage to use h2 -\> GetXaxis() -\> SetRangeUser for the histogram though. So it was definitely helpful and I got the right axes. Since I’m new, I’ll save this code for future reference, so I can learn more about ROOT. So for now, no more issues. Thanks for your help!
