Cannot make a TGraph2D plot

Hi,
I am trying to plot 4 points with lines connected. I use the following code:

  double *x = new double[4];
  double *y = new double[4];
  double *z = new double[4];
  x[0] = 6215.736328;
  x[1] = 5542.363770;
  x[2] = 5518.736816;
  x[3] = 5518.736816;

  y[0] = 3853.795410;
  y[1] = 5270.174805;
  y[2] = 5329.190430;
  y[3] = 5329.190430;
  
  z[0] = 2723.959961;
  z[1] = 2723.959961;
  z[2] = 2723.959961;
  z[3] = 2723.959961;
  
  TGraph2D * g = new TGraph2D(4, x, y, z);

  g->Draw("LINE");

However, I was given errors like:

the problem seems from the Z values. Maybe all points are in the same plane? so I do not know why I cannot plot the points even they are in the same plane. any hint?

Thanks,
Zhiyi.

Hi, I tried to plot it first using g->Draw(); and them g->Draw(“LINE”); and I get the following image (see below).


Interesting. Thanks for this little trick. :slight_smile:

Which root version are you using? I tried your approach in

root_v5.26
root_v5.23
root_v5.20

. All of them give me the same error that I posted above.

Cheers,
Zhiyi.

Yes, you get these errors because the Z values are all the same. I will fix that. If you only want to draw a line you do not need a TGraph2D. TGraph2D are not meant to be used that way (see the doc). Nevertheless what you tried should work and, as I said, I will fix it. Meanwhile I would suggest you use 3D polyline as it is what you are looking for:

{
   double *x = new double[4];
   double *y = new double[4];
   double *z = new double[4];
   x[0] = 6215.736328;
   x[1] = 5542.363770;
   x[2] = 5518.736816;
   x[3] = 5518.736816;

   y[0] = 3853.795410;
   y[1] = 5270.174805;
   y[2] = 5329.190430;
   y[3] = 5329.190430;

   z[0] = 2723.959961;
   z[1] = 2723.959961;
   z[2] = 2723.959961;
   z[3] = 2723.959961;

   TPolyLine3D *line3D = new TPolyLine3D (4,x,y,z) ;

   TH2F* histo = new TH2F("histo","", 1,5518.,6216.,1,3853.,5330.) ;
   histo->SetStats(kFALSE) ;
   histo->SetMinimum(2723.959960) ;
   histo->SetMaximum(2723.959962) ;

   TCanvas *canvas = new TCanvas("canvas","canvas",0,0,1000,618) ;
   canvas->SetFrameFillColor(30);
   canvas->SetTheta(23.) ; canvas->SetPhi(-23.) ;
   gPad->SetLeftMargin(0.18) ;
   gPad->SetRightMargin(0.10) ;
   gPad->SetTopMargin(0.20) ;
   gPad->SetBottomMargin(0.20) ;

   histo->Draw("lego0,fb") ;
   line3D->Draw() ;
}

This is now fixed in the SVN trunk.
root.cern.ch/viewvc?view=rev&revision=32105
thanks for reporting.