TGraph2D - palette underestimates values given on z axis

Hello,

The palette displays a shifted scale on the values on the z axis on a TGraph2D. It shows as if the ploted values were smaller than what I entered. I tried using a TH2 object, and the values are matching the palette scale, but using a TGraph2D is more appropriate for my work. I did tests on a simplified code. I show below the output that I rotated to display the z-axis in parallel with the palette scale.

I also tried to increase the default “NunberContours” – same issue.

I would be grateful if someone can tell me how to correct this shift in the palette scale.

Cheers,

Richard.

_ROOT Version: 6.24/06
_Platform: Ubuntu 20.04.4 LTS
Compiler: gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)

{
   const Int_t N = 10;

   Float_t x[N]= {1. ,  2. ,  3. , 4. ,  5. ,  6.  , 7.   ,  8.,  9. , 10.  };
   Float_t y[N]= {10., 12. , 11. , 8. , 10. , 10.  , 12.  , 11.,  8. , 10.  };
   Float_t z[N]= {0.9,  0.2,  0.4, 0.7,  0.6,  0.95,  0.33,  1.0, 0.5,  0.99};
   
   gStyle->SetPalette(1);

   TGraph2D *g=new TGraph2D(N,x,y,z);
   g->SetMarkerStyle(kFullCircle);
   g->SetMarkerSize(2);
   g->Draw("PCOLZ");
   gPad->SetGridx(); // not working 
   gPad->SetGridy(); // not working 
   //g->SetMinimum(0.2);
   //g->SetMaximum(1.0);
   gPad->SetTheta(90.); gPad->SetPhi(0.001);
   g->GetXaxis()->SetTitle("x axis");
   g->GetYaxis()->SetTitle("y axis");
   g->GetZaxis()->SetTitle("z axis");

   c1->Print("Test_Graph_2D.png");
 }

Hi and welcome to the root forum.

I used gStyle->SetNumberContours(160);
The results seems fine (have a look to the figure attached):

I do not know precisely how the colors are assigned to each marker, maybe there is some rounding issue.

Stefano

Hi Stefano,
Thank you for your feedback. Using this value of 160 is not perfect for me (some difference with your figure) but it makes my plot acceptable now.
Regards,
Richard.

If you only use g->SetMaximum(1.0); without SetMinimum, it looks better:

{
  TCanvas *c1 = new TCanvas();
  const Int_t N = 10;

  Float_t x[N]= {1. , 2. , 3. , 4. , 5. , 6. , 7. , 8., 9. , 10. };
  Float_t y[N]= {10., 12. , 11. , 8. , 10. , 10. , 12. , 11., 8. , 10. };
  Float_t z[N]= {0.9, 0.2, 0.4, 0.7, 0.6, 0.95, 0.33, 1.0, 0.5, 0.99};

  gStyle->SetPalette(1);

  TGraph2D *g=new TGraph2D(N,x,y,z);
  g->SetMarkerStyle(kFullCircle);
  g->SetMarkerSize(2);
  g->Draw("PCOLZ");
  //g->SetMinimum(0.2);
  g->SetMaximum(1.0);
  gPad->SetTheta(0.); gPad->SetPhi(0.);
  g->GetXaxis()->SetTitle("x axis");
  g->GetYaxis()->SetTitle("y axis");
  g->GetZaxis()->SetTitle("z axis");

  c1->Print("Test_Graph_2D.png");
  }


But:

  • it could be a coincidence in this case, which maybe (I don’t know) could be replicated in other cases by carefully controlling the range in z and the number of contours;
  • the minimum goes to 0, which you might not want in some cases;
  • I’m not sure whether the values laying exactly on the value between colours should belong to the lower or higher colour (in the plot, they go with the lower);

so maybe using a lot of contours is better in general.

{
   const Int_t N = 10;

   Float_t x[N]= {1. ,  2. ,  3. , 4. ,  5. ,  6.  , 7.   ,  8.,  9. , 10.  };
   Float_t y[N]= {10., 12. , 11. , 8. , 10. , 10.  , 12.  , 11.,  8. , 10.  };
   Float_t z[N]= {0.9,  0.2,  0.4, 0.7,  0.6,  0.95,  0.35,  1.0, 0.5,  0.97};

   gStyle->SetPalette(1);

   TGraph2D *g=new TGraph2D(N,x,y,z);
   g->SetMarkerStyle(kFullCircle);
   g->SetMarkerSize(2);
   g->Draw("PCOLZ");

   g->SetMaximum(1.0);
   g->SetMinimum(0.0);
   g->GetZaxis()->SetNdivisions(-20);
   g->GetZaxis()->SetTitleOffset(1.5);
   gPad->SetTheta(0.01); gPad->SetPhi(0.01);
   g->GetXaxis()->SetTitle("x axis");
   g->GetYaxis()->SetTitle("y axis");
   g->GetZaxis()->SetTitle("z axis");
}