TGraph2D axis titles and values not showing

As the title suggests, when plotted, all 3 axes are left blank. I have settings on, so I’m not sure why this is happening.

Here is my code for when I create the TGraph2D’s and when I’m drawing it:

[code]
TGraph2D g_Prop;
TGraph2D earth;

g_Prop.SetTitle(“Cosmic Ray propagation through the galaxy;x (kpc);y (kpc);z (kpc)”);

earth.SetTitle(“Cosmic Ray propagation through the galaxy;x (kpc);y (kpc);z (kpc)”);

TApplication theApp(“app”, &argc, argv);

TCanvas c_Prop(“c_Prop”,“Cosmic Ray Propagation Through The Galaxy”, canvasX, canvasY);
g_Prop.SetMarkerColor(kBlue);
g_Prop.SetMarkerStyle(kFullDotMedium);
g_Prop.SetMarkerSize(1);
g_Prop.GetXaxis()->SetTitle(“x (kpc)”);
g_Prop.GetYaxis()->SetTitle(“y (kpc)”);
g_Prop.GetZaxis()->SetTitle(“z (kpc)”);
g_Prop.GetXaxis()->SetRangeUser(xmin,xmax);
g_Prop.GetYaxis()->SetRangeUser(ymin,ymax);
g_Prop.GetZaxis()->SetRangeUser(zmin,zmax);
g_Prop.Draw(“AP”);
earth.SetMarkerColor(kRed);
earth.SetMarkerStyle(kFullDotLarge);
earth.SetMarkerSize(1);
earth.GetXaxis()->SetRangeUser(xmin,xmax);
earth.GetYaxis()->SetRangeUser(ymin,ymax);
earth.GetZaxis()->SetRangeUser(zmin,zmax);
earth.GetXaxis()->SetTitle(“x (kpc)”);
earth.GetYaxis()->SetTitle(“y (kpc)”);
earth.GetZaxis()->SetTitle(“z (kpc)”);
earth.Draw(“P SAME”);

theApp.Run();
return(0); [/code]

and see the picture for the output.

The blue dots seen are randomised each run, so the propagation changes every time. Not sure if that has something to do with it; it shouldn’t. g_Prop is the TGraph2D containing the blue dots, whilst earth just contains the one point.

As you can see (hopefully, maybe), the axes are self-scaling, even though I have set limits for them (take my word for it!).

Notice I have also attempted to set the axis titles twice: Once in the SetTitle of the graph, and then separately on the canvas. Still to no avail.

I can’t show you the whole code, because in truth the code uses I think 4 or 5 other .cpp files, so it would be really long and messy. This means you can’t reproduce the plot, but I’m hoping what I have shown you will be enough.

If you need anymore information, please ask.

Any and all help is much appreciated.

Joshua


Remove option A:

  g_Prop.Draw("P");

Oh great, thanks! Why does that work though?

And also, even though now yes the axis values and titles are showing, they’re still not setting to the range I put them in. I clearly have


  const double xmin = -20.0;
  const double xmax = 20.0;

  const double ymin = -20.0;
  const double ymax = 20.0;

  const double zmin = -5.0;
  const double zmax = 5.0;

  ...

  g_Prop.GetXaxis()->SetRangeUser(xmin,xmax);
  g_Prop.GetYaxis()->SetRangeUser(ymin,ymax);
  g_Prop.GetZaxis()->SetRangeUser(zmin,zmax);
  earth.GetXaxis()->SetRangeUser(xmin,xmax);
  earth.GetYaxis()->SetRangeUser(ymin,ymax);
  earth.GetZaxis()->SetRangeUser(zmin,zmax);

Yet the axes are still self-scaling (see attachment).


May be:

g_Prop.GetHistogram()->GetXaxis()->SetRangeUser(xmin,xmax);

Hmm, still no luck unfortunately

Propagation.cc:170:24: error: invalid use of incomplete type ‘class TH2D’
   g_Prop.GetHistogram()->GetXaxis()->SetRangeUser(xmin,xmax);
                        ^

This works for me:

void graph2dp()
{
   TCanvas *c = new TCanvas("c","Graph3D example",0,0,700,600);

   Double_t P = 6.;
   Int_t np   = 500;    // generate this many nodes

   Double_t *rx=0, *ry=0, *rz=0;
   rx = new Double_t[np];
   ry = new Double_t[np];
   rz = new Double_t[np];

   TRandom *r = new TRandom();

   for (Int_t N=0; N<np; N++) {
      rx[N]=2*P*(r->Rndm(N))-P;
      ry[N]=2*P*(r->Rndm(N))-P;
      rz[N]=(sin(rx[N])/rx[N])*(sin(ry[N])/ry[N])+0.2;
   }
   TGraph2D *dt = new TGraph2D(np, rx, ry, rz);
  // dt->Project("xy")->Draw("contz");
   dt->Draw("P0");

   dt->GetXaxis()->SetTitle("X Title");
   dt->GetYaxis()->SetTitle("Y Title");
   dt->GetZaxis()->SetTitle("Z Title");
   dt->GetXaxis()->SetRangeUser(0.,2.);
}