TGraph2D not showing Axis Labels

Hi everyone

Looks like TGraph2D is not showing axis labels:

  TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,400);
  Double_t x, y, z, P = 6.;
  Int_t np = 200;
  TGraph2D *dt = new TGraph2D();
  TRandom *r = new TRandom();
  for (Int_t N=0; N<np; N++) {
    x = 2*P*(r->Rndm(N))-P;
    y = 2*P*(r->Rndm(N))-P;
    z = (sin(x)/x)*(sin(y)/y)+0.2;
    dt->SetPoint(N,x,y,z);
  }

  dt->GetXaxis()->SetTitle("X-Axis");
  dt->GetYaxis()->SetTitle("Y-Axis");
  dt->GetZaxis()->SetTitle("Z-Axis");
  gStyle->SetPalette(1);
  dt->Draw("surf1");

Working with ROOT 6.11 on Linux

Andres

  dt->GetHistogram()->GetXaxis()->SetTitle("X-Axis");
  dt->GetHistogram()->GetYaxis()->SetTitle("Y-Axis");
  dt->GetHistogram()->GetZaxis()->SetTitle("Z-Axis");
1 Like

Thanks @Wile_E_Coyote, it works. @couet could be this included so it can be called directly from dt->GetXaxis()->SetTitle("X-Axis"); without calling the histogram?

There is an even simpler way not needing the intermediate histogram:

dt->SetTitle("Graph title; X axis title; Y axis title; Z axis title");

Yup. That also works. I just thought Get*axis()->SetTitle() should work as it works for TH2D and everything else :smile:

1 Like

Yes but TH2D has many Setters. They all can be accessed as suggested before (via GetHistogram()) . I do not think it would make sense to duplicate all of them in TGraph2D… In the case of titles (which is a frequent case) an easy and efficient way already exists (as i explained it). I will update the doc to make it easier to find.

I agree. If it’s also included in the examples (here) will be useful, most of them do not have labels :sweat_smile:. I remembered the first time I saw how to set axis labels was like that and I kept using it. Thanks!

I agree. Done. it will show tomorrow in the doc online.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.