# TGraph2D - palette underestimates values given on z axis

**URL:** <https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818>\
**Category:** Newbie\
**Created:** [July 15, 2022, 10:58am UTC](https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818 "2022-07-15T10:58:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_B](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/85f322/32.png) [@Richard\_B](https://root-forum.cern.ch/u/Richard_B)\
**Post date:** [July 15, 2022, 10:58am UTC](https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818/1 "2022-07-15T10:58:43Z")

</div>

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)

```auto
{
   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");
 }

```

 ![Test_Graph_2D_x_z__legend](https://root-forum.cern.ch/uploads/default/original/3X/7/0/70bd31fa3d6113f19a75e7ed5319a14716082bb8.png)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [July 15, 2022, 2:34pm UTC](https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818/2 "2022-07-15T14:34:16Z")

</div>

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

 ![Schermata 2022-07-15 alle 16.23.07](https://root-forum.cern.ch/uploads/default/original/3X/7/9/791a8e3f1474f6fc643297191e7cd4626c3cd08f.png)

---

<div class="post-metadata">

**Author:** ![Richard\_B](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/85f322/32.png) [@Richard\_B](https://root-forum.cern.ch/u/Richard_B)\
**Post date:** [July 17, 2022, 2:45pm UTC](https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818/3 "2022-07-17T14:45:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [July 18, 2022, 5:38am UTC](https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818/4 "2022-07-18T05:38:25Z")

</div>

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

```auto
{
  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");
  }

```

 ![Test_Graph_2D](https://root-forum.cern.ch/uploads/default/original/3X/4/a/4a759c5774322783f84aa74b37c34c11333aaf2f.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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 20, 2022, 2:54pm UTC](https://root-forum.cern.ch/t/tgraph2d-palette-underestimates-values-given-on-z-axis/50818/5 "2022-07-20T14:54:35Z")

</div>

```auto
{
   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");
}

```

 ![Screenshot 2022-07-20 at 16.53.43](https://root-forum.cern.ch/uploads/default/original/3X/c/7/c7911b834764fc65b98667c74b869ba7bb2ee27b.png)
