Setting axes on TGraph2D (cont 4z)

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


You mean changing the axis limits ? which axis ?

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()

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);
   auto gr2 = new TGraph2D(nd);

   // Fill the 2D graph
   Double_t zmax = 0;
   for (Int_t i=0; i<nd; i++) {
      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->Draw("cont4 z");
   TH2D *h2 = gr2->GetHistogram();

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

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

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!