2D scatter plot using TGraph2D


I am trying to generate a 2D scatter plot, where the point colors are given by their z-value.
The best I could was to draw a 3D plot and project it into the 2D plane:

TGraph2D *gr = new TGraph2D(n,xpts,yptx,zpts); gr->Draw("PCOL"); gPad->SetTheta(90.); gPad->SetPhi(0.001);

and it seems to work fine.
The only thing missing is the ability to generate and draw a palette in the x-y plane showing the z-axis color code.
Any suggestions?




I know this is a very old thread but I have a similar problem.
Instead of TGraph2D I have a TH2D histogram. As soon as I specify the “COLZ” drawing option, the points are substituted by horizontal bars. If I do not specify the COLZ option, I get a scatter-plot but without the color palette.
Options that I have tried without success:

  • COLZ
  • SCAT

TH2D “SCAT” option is not recommended. COLZ draws each cell as a box. The color of the box depends on its content and is picked in a current color map. I do not really understand what you mean by “substituted by horizontal bars” … Generally speaking you will find all the drawing options for 2D histograms here.

By “horizontal bar” I mean just a box as you say. I want the same effect but with a point instead of a box.

For TGraph2D this is the PCOL option. It produces a 3D plot. This does not exist for TH2D. All the TH2D options are listed in the page I posted previously. Of course a proper C++ macro can be made to produce such drawing, But that’s not a single drawing option.

1 Like